Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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