Skip to content

Commit ced3110

Browse files
committed
test(algorithms, sliding-window): longest repeating char replacement
1 parent 012f74c commit ced3110

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

algorithms/sliding_window/longest_repeating_char_replacement/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def character_replacement(s: str, k: int) -> int:
2626
2727
At this point, the last valid window has moved one step to the right, but it might still be invalid. As explained
2828
earlier, we are only interested in larger windows, so we don't need to decrease the window size. We move the window
29-
of size i−1 further and further to the right until it becomes valid again.
29+
of size l−1 further and further to the right until it becomes valid again.
3030
3131
If we come across a valid window, we try to expand it as much as possible, and the process continues until the right
3232
pointer reaches the rightmost alphabet of the string. At this point, the size of the window indicates the longest
@@ -40,9 +40,11 @@ def character_replacement(s: str, k: int) -> int:
4040
are m unique characters, then the memory required is proportional to m. So the space complexity is O(m). Considering
4141
uppercase English letters only, m=26
4242
43-
@param s: input string
44-
@param k: number of replacements
45-
@return: length of the longest substring with repeating characters after at most k replacements
43+
Args:
44+
s(str): input string
45+
k(int): number of replacements
46+
Returns:
47+
int: length of the longest substring with repeating characters after at most k replacements
4648
"""
4749
frequency_map = {}
4850
result = 0

algorithms/sliding_window/longest_repeating_char_replacement/test_longest_repeating_character_replacement.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,69 @@ def test_ABCCDC_with_1(self):
2828
actual = character_replacement(s, k)
2929
self.assertEqual(expected, actual)
3030

31+
def test_aaacbbbaabab(self):
32+
s = "aaacbbbaabab"
33+
k = 2
34+
expected = 6
35+
actual = character_replacement(s, k)
36+
self.assertEqual(expected, actual)
37+
38+
def test_abbcab(self):
39+
s = "abbcab"
40+
k = 2
41+
expected = 5
42+
actual = character_replacement(s, k)
43+
self.assertEqual(expected, actual)
44+
45+
def test_dippitydip(self):
46+
s = "dippitydip"
47+
k = 4
48+
expected = 6
49+
actual = character_replacement(s, k)
50+
self.assertEqual(expected, actual)
51+
52+
def test_coollooc(self):
53+
s = "coollooc"
54+
k = 2
55+
expected = 6
56+
actual = character_replacement(s, k)
57+
self.assertEqual(expected, actual)
58+
59+
def test_aaaaaaaaaa(self):
60+
s = "aaaaaaaaaa"
61+
k = 2
62+
expected = 10
63+
actual = character_replacement(s, k)
64+
self.assertEqual(expected, actual)
65+
66+
def test_lmno(self):
67+
s = "lmno"
68+
k = 2
69+
expected = 3
70+
actual = character_replacement(s, k)
71+
self.assertEqual(expected, actual)
72+
73+
def test_xxxxx(self):
74+
s = "xxxxx"
75+
k = 1
76+
expected = 5
77+
actual = character_replacement(s, k)
78+
self.assertEqual(expected, actual)
79+
80+
def test_fzffz(self):
81+
s = "fzfzfz"
82+
k = 6
83+
expected = 6
84+
actual = character_replacement(s, k)
85+
self.assertEqual(expected, actual)
86+
87+
def test_aabccbb(self):
88+
s = "aabccbb"
89+
k = 2
90+
expected = 5
91+
actual = character_replacement(s, k)
92+
self.assertEqual(expected, actual)
93+
3194

3295
if __name__ == "__main__":
3396
unittest.main()

algorithms/sliding_window/longest_substring_with_k_repeating_chars/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def longest_substring(s: str, k: int) -> int:
6060
6161
Complexity Analysis
6262
63-
Time Complexity: O(N^2), where N is the length of string ss. Though the algorithm performs better in most cases,
63+
Time Complexity: O(N^2), where N is the length of string s. Though the algorithm performs better in most cases,
6464
the worst case time complexity is still (N ^ 2).
6565
6666
In cases where we perform split at every index, the maximum depth of recursive call could be O(N). For each
@@ -69,9 +69,11 @@ def longest_substring(s: str, k: int) -> int:
6969
Space Complexity: O(N) This is the space used to store the recursive call stack. The maximum depth of recursive
7070
call stack would be O(N).
7171
72-
@param s: String to evaluate for
73-
@param k: length of the longest substring
74-
@return: length of longest substring with at most repeating characters of length k
75-
@rtype int
72+
Args:
73+
s: String to evaluate for
74+
k: length of the longest substring
75+
76+
Returns:
77+
length of longest substring with at most repeating characters of length k
7678
"""
7779
return longest_substring_util(s, 0, len(s), k)

0 commit comments

Comments
 (0)