Wednesday, May 3, 2017

LeetCode 2nd time 3. Longest Substring Without Repeating Characters LeetCode Java

 public int lengthOfLongestSubstring(String s) {  
     if(s==null || s.length()==0) return 0;  
     int maxL=1;  
     int i=0;  
     int j=0;  
     // Set<Character> dupCheck=new HashSet<Character>();  
     Map<Character,Integer> dupCheck=new HashMap<Character,Integer>();  
     while(i<s.length()&&j<s.length()){  
         if(dupCheck.containsKey(s.charAt(j)) && dupCheck.get(s.charAt(j))>=i){  
           i=dupCheck.get(s.charAt(j))+1;  
         }  
        else {  
          maxL=Math.max(maxL,j-i+1);  
        }  
        dupCheck.put(s.charAt(j),j);  
        j++;  
       }  
     return maxL;  
   }  

No comments:

Post a Comment