My Java solution, simple and clear


  • 0
    J
       public int lengthOfLongestSubstring(String s) {
            if(s==null || s.equals("")){
                return 0;
            }
            int head = 0;
            int tail = 0;
            int len = 1;
            for(int i = 0; i < s.length() && tail < s.length(); i++){
                for(int j = head; j< tail; j++){
                    if(String.valueOf(s.charAt(j)).equals(String.valueOf(s.charAt(tail)))){
                        head = j+1;
                        break;
                    }
                }
                if(tail - head >= len){
                    len = tail - head + 1;
                }
                tail += 1;
            }
            return len;
        }

Log in to reply
 

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