Last active
August 29, 2015 14:00
-
-
Save gabhi/11243698 to your computer and use it in GitHub Desktop.
length of longest substring without repeating characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int lengthOfLongestSubstring(String s) { | |
Boolean [] exist =new Boolean [256]; | |
for (int i=0; i<256;i++){ | |
exist[i]=false; | |
} | |
int i=0,j=0,n=s.length(),maxlen=0; | |
while(j<n){ | |
if (exist[s.charAt(j)]){ | |
maxlen=Math.max(maxlen,j-i); | |
while(s.charAt(i)!=s.charAt(j)){ | |
exist[s.charAt(i)]=false; | |
i++; | |
} | |
i++;j++; | |
} | |
else{ | |
exist[s.charAt(j)]=true; | |
j++; | |
} | |
} | |
return maxlen=Math.max(maxlen, n-i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment