Skip to content

Instantly share code, notes, and snippets.

@linzhp
Created March 15, 2013 04:23
Show Gist options
  • Save linzhp/5167470 to your computer and use it in GitHub Desktop.
Save linzhp/5167470 to your computer and use it in GitHub Desktop.
public class Solution {
public int lengthOfLongestSubstring(String s) {
// Start typing your Java solution below
// DO NOT write main() function
String longest = "";
for(int i = 0; i < s.length(); i++) {
HashSet<Character> set = new HashSet<Character>();
set.add(s.charAt(i));
int j;
for(j = i + 1; j < s.length(); j++) {
char current = s.charAt(j);
if(set.contains(current)) {
break;
} else {
set.add(current);
}
}
if(j - i > longest.length()) {
longest = s.substring(i, j);
}
}
return longest.length();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment