Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exhesham/14502bd54387c52073b0280a5b87eeac to your computer and use it in GitHub Desktop.
Save exhesham/14502bd54387c52073b0280a5b87eeac to your computer and use it in GitHub Desktop.
interview article code
public int lengthOfLongestSubstring(String s) {
HashMap<Character,Integer> set = new HashMap<>();
int max = 0;int j=0;
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(set.containsKey(c)){
// as we dont delete history, we dont
// want to jump back to index in a substring that is dead
j = Math.max(j, set.get(c)+1);
}
set.put(c,i);
max = Math.max(i-j+1, max);
}
return max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment