How about this java solution?


  • 0
    public static int lengthOfLongestSubstring(String s) {
            String subString = "";
            int maxLength = 0;
            int index = 0;
            for(int i = 0; i < s.length(); i++) {
                index = subString.indexOf(s.charAt(i));
                subString = subString.substring(index + 1) + s.charAt(i);
                maxLength = Math.max(subString.length(), maxLength);
            }
            return maxLength;
        }
    

  • 0
    L

    @何六 It's interesting


  • 0
    M

    It's a different way of doing it and sure is interesting. But, indexOf is O(n) and just that makes it O(n^2) solution. Correct me if i am wrong.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.