Created
March 21, 2014 22:30
-
-
Save rayjcwu/9697846 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 uniqueLength(String str) { | |
| char[] s = str.toCharArray(); | |
| Set<Character> showed = new HashSet<Character>(); | |
| int front = 0; | |
| int back = 0; | |
| int maxLen = 0; | |
| // unique substring [back: front - 1] | |
| while (front < s.length) { | |
| if (!showed.contains(s[front])) { | |
| showed.add(s[front]); | |
| front++; | |
| maxLen = Math.max(maxLen, front - back); | |
| } else { | |
| showed.remove(s[back]); | |
| back++; | |
| } | |
| } | |
| return maxLen; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment