JAVA------------------------Easy Version To Understad!!!!!!!!!!!!


  • -1
    H
     public static int lengthOfLongestSubstring(String s) {
    	if(s==null||s.length()==0)
    		return 0;
    	int len=s.length(),maxLen=0,slow=0,fast=0;
    	HashMap<Character, Integer> map=new HashMap<Character, Integer>();
    	for(;fast<len;fast++){
    		char currentChar=s.charAt(fast);
    		if(map.get(currentChar)==null){
    			map.put(currentChar, fast);
    			maxLen=Integer.max(maxLen, fast-slow+1);
    		}
    		else{
    			int lastPosition=map.get(currentChar);
    			if(lastPosition<slow){
    				maxLen=Integer.max(maxLen, fast-slow+1);
    				map.put(currentChar, fast);
    			}
    			else{
    				slow=lastPosition+1;
    				map.put(currentChar, fast);
    			}
    		} 		
    	}
        return maxLen;
    }

Log in to reply
 

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