(3)Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
O(NlogN)
The difficulty is to define the boundaries of repeated elements
public static int LengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
HashSet<Character> set = new HashSet<Character>();
int leftBound = 0, max = 0;
for (int i = 0; i < s.length(); i++) {
if (set.contains(s.charAt(i))) {
while (leftBound < i && s.charAt(leftBound) != s.charAt(i)) {
set.remove(s.charAt(leftBound));
leftBound++;
}
leftBound++;
} else {
set.add(s.charAt(i));
max = Math.max(max, i - leftBound + 1);
}
}
return max;
}
浙公网安备 33010602011771号