Skip to content

Instantly share code, notes, and snippets.

@gabhi
Last active August 29, 2015 14:00
Show Gist options
  • Save gabhi/11243698 to your computer and use it in GitHub Desktop.
Save gabhi/11243698 to your computer and use it in GitHub Desktop.
length of longest substring without repeating 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