Last active
March 23, 2018 13:57
-
-
Save exhesham/14502bd54387c52073b0280a5b87eeac to your computer and use it in GitHub Desktop.
interview article code
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) { | |
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