Solution using a pointer for beginIndex


  • 0
    C
    public int lengthOfLongestSubstring(String s) {
    int[] arr = new int[128];
    Arrays.fill(arr, -1);
    int max = 0;
    int beginIndex=0;
    for(int i=0; i<s.length(); i++){
    	char c = s.charAt(i);
    	if(arr[c]>=beginIndex){
    		max = Math.max(max,i-beginIndex);
    		beginIndex = arr[c]+1;
    	}
    	arr[c]=i;
    }
    max = Math.max(max,s.length()-beginIndex);
    return max;
    }

Log in to reply
 

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