Beats 92% 43ms Two pointer solution


  • 1
    S
    public int lengthOfLongestSubstring(String s) {
            int start = -1, end = 0, max = 0;
    	int[] letter = new int[128];
    	Arrays.fill(letter, -1);
    	char[] c = s.toCharArray();
    	while (end < c.length) {
    		int id = c[end]-' ';
    		if (letter[id] == -1){
    			max = Math.max(max, end-start);
    		}else {
    			while(++start < letter[id])
    				letter[c[start]-' '] = -1;
    		}
    		letter[id] = end++;
    	}
    	return max;
    }
    

Log in to reply
 

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