Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
Created March 21, 2014 22:30
Show Gist options
  • Select an option

  • Save rayjcwu/9697846 to your computer and use it in GitHub Desktop.

Select an option

Save rayjcwu/9697846 to your computer and use it in GitHub Desktop.
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