Skip to content

Commit fd35635

Browse files
committed
refactor(algorithms): sliding window longest substring
1 parent 1b90fe5 commit fd35635

File tree

10 files changed

+77
-33
lines changed

10 files changed

+77
-33
lines changed

pystrings/longest_repeating_char_replacement/README.md renamed to algorithms/sliding_window/longest_repeating_char_replacement/README.md

File renamed without changes.

pystrings/longest_repeating_char_replacement/__init__.py renamed to algorithms/sliding_window/longest_repeating_char_replacement/__init__.py

File renamed without changes.

pystrings/longest_repeating_char_replacement/test_longest_repeating_character_replacement.py renamed to algorithms/sliding_window/longest_repeating_char_replacement/test_longest_repeating_character_replacement.py

File renamed without changes.

pystrings/longest_substring_with_k_repeating_chars/README.md renamed to algorithms/sliding_window/longest_substring_with_k_repeating_chars/README.md

File renamed without changes.

pystrings/longest_substring_with_k_repeating_chars/__init__.py renamed to algorithms/sliding_window/longest_substring_with_k_repeating_chars/__init__.py

File renamed without changes.

pystrings/longest_substring_with_k_repeating_chars/test_longest_substring_k_repeating_chars.py renamed to algorithms/sliding_window/longest_substring_with_k_repeating_chars/test_longest_substring_k_repeating_chars.py

File renamed without changes.

pystrings/longest_substring_without_repeating_characters/README.md renamed to algorithms/sliding_window/longest_substring_without_repeating_characters/README.md

File renamed without changes.

pystrings/longest_substring_without_repeating_characters/__init__.py renamed to algorithms/sliding_window/longest_substring_without_repeating_characters/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ def length_of_longest_substring(s: str) -> int:
1313
Complexity Analysis:
1414
Time: O(n) where n is the length of the string as we travers characters in the string
1515
Space: O(n) as we are storing the characters visited in a dictionary
16-
17-
@param s: input string
18-
@return: length of the longest substring without repeating characters
16+
Args:
17+
s (str): input string
18+
Return:
19+
int: length of the longest substring without repeating characters
1920
"""
2021
visited = {}
2122
max_length = 0
2223
left = 0
24+
length = len(s)
2325

24-
for right in range(len(s)):
26+
for right in range(length):
2527
char = s[right]
2628

2729
if char in visited:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import unittest
2+
from . import length_of_longest_substring
3+
4+
5+
class LongestSubstringWithoutRepeatingCharactersTestCase(unittest.TestCase):
6+
def test_abcabcbb(self):
7+
"""abcabcbb should return 3"""
8+
s = "abcabcbb"
9+
expected = 3
10+
actual = length_of_longest_substring(s)
11+
self.assertEqual(expected, actual)
12+
13+
def test_bbbbb(self):
14+
"""bbbbb should return 1"""
15+
s = "bbbbb"
16+
expected = 1
17+
actual = length_of_longest_substring(s)
18+
self.assertEqual(expected, actual)
19+
20+
def test_pwwkew(self):
21+
"""pwwkew should return 3"""
22+
s = "pwwkew"
23+
expected = 3
24+
actual = length_of_longest_substring(s)
25+
self.assertEqual(expected, actual)
26+
27+
def test_bbbbbb(self):
28+
"""bbbbbb should return 1"""
29+
s = "bbbbbb"
30+
expected = 1
31+
actual = length_of_longest_substring(s)
32+
self.assertEqual(expected, actual)
33+
34+
def test_empty_string(self):
35+
"""'' should return 0"""
36+
s = ""
37+
expected = 0
38+
actual = length_of_longest_substring(s)
39+
self.assertEqual(expected, actual)
40+
41+
def test_abcdbea(self):
42+
"""abcdbea should return 5"""
43+
s = "abcdbea"
44+
expected = 5
45+
actual = length_of_longest_substring(s)
46+
self.assertEqual(expected, actual)
47+
48+
def test_aba(self):
49+
"""aba should return 2"""
50+
s = "aba"
51+
expected = 2
52+
actual = length_of_longest_substring(s)
53+
self.assertEqual(expected, actual)
54+
55+
def test_abccabcabcc(self):
56+
"""abccabcabcc should return 3"""
57+
s = "abccabcabcc"
58+
expected = 3
59+
actual = length_of_longest_substring(s)
60+
self.assertEqual(expected, actual)
61+
62+
def test_aaaabaaa(self):
63+
"""aaaabaaa should return 2"""
64+
s = "aaaabaaa"
65+
expected = 2
66+
actual = length_of_longest_substring(s)
67+
self.assertEqual(expected, actual)
68+
69+
70+
if __name__ == "__main__":
71+
unittest.main()

pystrings/longest_substring_without_repeating_characters/test_longest_substring_without_repeating_characters.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)