Sliding Window Approach Beats 95.60%


  • 0
    
        public int lengthOfLongestSubstring(String s) {
    	        int maxlen = 0;
    	        int begin = 0;
    	        int[] table = new int[256];
    	        char[] sc = s.toCharArray();
    	        for(int i = 0 ; i < s.length(); i++){
    	        	int index = sc[i];
    	        	if(table[index] != 0) {
    	        		while(table[index] != 0){
    	        			table[sc[begin]]--;
    	        			begin++;
    	        		}
    	        	}
    	        	table[index]++;
            		maxlen = Math.max(maxlen, i - begin+1);
    	        }
    	        return maxlen;
    	    }
    
    

Log in to reply
 

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