If s = "abcdeeabcd", the lengthOfLongestSubstring is 5.this answer is wrong ,but OJ can pass


  • -1
    1
    public static int lengthOfLongestSubstring(String s) {
            Set<Character> set = new HashSet() ;
            int i = 0 ;
            int j = 0 ;
            int maxLen = 0 ;
            char[] sArray = s.toCharArray() ;
            int len = s.length() ;
            while (j < len){
                if (set.contains(sArray[j])){
                    maxLen = Math.max(maxLen,j-i) ;
                    while(sArray[i] != sArray[j]){
                       set.remove(sArray[i]) ; 
                        i++ ;
                    }
                    i++ ;
                    j++ ;
                }else{
                    set.add(sArray[j]) ;
                    j++;
                }
                
            }
            maxLen = Math.max(maxLen ,len -i) ;
            return maxLen ;
        }

  • 0
    J

    The answer is 5. the longest substring is "abcde" or "eabcd".


Log in to reply
 

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