Created
March 15, 2013 04:23
-
-
Save linzhp/5167470 to your computer and use it in GitHub Desktop.
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 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