Cannot pass the "aab" case. HELP!


  • 0
    M

    enter image description here

    public class Solution {

    public int lengthOfLongestSubstring(String s) {
        if(s.length()==0)  
            return 0;
        
        if(s.length()==2&&s.charAt(0)==s.charAt(1)){
            return 1;
        }
        else if(s.length()==1)
            return 1;
        else if(s.length()>=2)
            return length1(s);
       return 1;     
    }
    
    public int length1(String s){
         HashMap<Character,Integer> map1= new HashMap<Character,Integer>();
       
        for(int i=subS(s);i<s.length();i++){
            
            if(!map1.containsKey(s.charAt(i))){
                map1.put(s.charAt(i),i);
                if(i==s.length()-1){
                    return i+1;
                }
             }
          else if(map1.containsKey(s.charAt(i)) || i==s.length()-1)
                return i+1;
        }
        return 1;
    }
    

    // the following method use to count the starting point of the String that I need. For example: aabcccc will //return 1
    // aaabc will return 2

    public int subS(String s){
        HashMap<Character,Integer> map2= new HashMap<Character,Integer>();
        map2.put(s.charAt(0),0);
        for(int i=1;i<s.length();i++){
            if(!map2.containsKey(s.charAt(i))){
                return i-1;
            }
        }
        return 0;
    }
    

    }


Log in to reply
 

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