Skip to content

Commit 91d26d4

Browse files
committed
Minor fix
1 parent 062d4fc commit 91d26d4

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/main/java/com/leetcode/hashtables/slidingwindow/LongestSubstringWithKDistinctCharacters.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static int lengthOfLongestSubstringKDistinct(String str, int k) {
4848

4949
// when number of distinct characters in the window exceeds k:
5050
// - update length
51-
// - remove the first character in the window or reduce its count if the window had more than one of this character
51+
// - remove the first character in the window or reduce its count if the window has more than one of this character
5252
// - lastly, move the window forward
5353
if (letterCountInWindow.keySet().size() > k) {
5454
char firstChar = str.charAt(left);
@@ -64,17 +64,18 @@ public static int lengthOfLongestSubstringKDistinct(String str, int k) {
6464
right++;
6565
}
6666

67-
return length == 0 ? right - left : length;
67+
return Math.max(length, right - left);
6868
}
6969

7070
public static void main(String[] args) {
7171
assertEquals(3, lengthOfLongestSubstringKDistinct("eceba", 2));
7272
assertEquals(7, lengthOfLongestSubstringKDistinct("eceeeeeba", 2));
73+
assertEquals(12, lengthOfLongestSubstringKDistinct("bbbeeeeebaaaaaaaaaaa", 2));
7374
assertEquals(2, lengthOfLongestSubstringKDistinct("abcdef", 2));
7475
assertEquals(1, lengthOfLongestSubstringKDistinct("a", 1));
76+
assertEquals(0, lengthOfLongestSubstringKDistinct("aa", 0));
7577
assertEquals(2, lengthOfLongestSubstringKDistinct("aa", 1));
7678
assertEquals(3, lengthOfLongestSubstringKDistinct("aaa", 1));
77-
assertEquals(0, lengthOfLongestSubstringKDistinct("aa", 0));
7879
assertEquals(3, lengthOfLongestSubstringKDistinct("aab", 2));
7980
assertEquals(8, lengthOfLongestSubstringKDistinct("abcabcbb", 3));
8081
assertEquals(5, lengthOfLongestSubstringKDistinct("pwwkew", 3));

0 commit comments

Comments
 (0)