Skip to content

Commit 6feb53e

Browse files
committed
feat(algorithms, sliding-window): longest substring without duplication
1 parent 859a5dc commit 6feb53e

6 files changed

Lines changed: 64 additions & 127 deletions

File tree

algorithms/sliding_window/length_of_longest_substring/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,36 @@ def length_of_longest_substring_2(s: str) -> int:
5959
max_length = max(max_length, end - start + 1)
6060

6161
return max_length
62+
63+
64+
def longest_substring_without_duplication(s: str) -> str:
65+
"""
66+
This returns the longest substring without duplicate or repeating characters from the given substring.
67+
Args:
68+
s(str): provided string
69+
Returns:
70+
str: substring without repeating characters
71+
"""
72+
# creating a dictionary to store last positions of occurrence
73+
seen: Dict[str, int] = {}
74+
longest_substring = ""
75+
# staring the initial window at 0
76+
start = 0
77+
78+
for end, ch in enumerate(s):
79+
# have we seen this element already?
80+
if ch in seen:
81+
# last_index is the previous appearance of character 'ch'
82+
last_index = seen[ch]
83+
# move the start pointer to position after the last occurrence
84+
# We use max() to ensure start never moves backward (the previous occurrence might be before the current
85+
# window)
86+
start = max(start, last_index + 1)
87+
88+
# update last seen value of character
89+
seen[ch] = end
90+
# We check if the substring we have so far is longer than the previous substring and set that before continuing
91+
# with the iteration
92+
longest_substring = max(longest_substring, s[start : end + 1], key=len)
93+
94+
return longest_substring

algorithms/sliding_window/length_of_longest_substring/test_length_of_longest_substring.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
from parameterized import parameterized
33
from algorithms.sliding_window.length_of_longest_substring import (
44
length_of_longest_substring,
5-
length_of_longest_substring_2
5+
length_of_longest_substring_2,
66
)
77

88
LENGTH_OF_LONGEST_SUBSTRING = [
9+
("clementisacap", 8),
910
("abcabcbb", 3),
1011
("bbbbb", 1),
1112
("pwwkew", 3),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
from parameterized import parameterized
3+
from algorithms.sliding_window.length_of_longest_substring import (
4+
longest_substring_without_duplication,
5+
)
6+
7+
8+
LONGEST_SUBSTRING_WITHOUT_REPEATING_CHARACTERS = [
9+
("clementisacap", "mentisac"),
10+
("abcabcbb", "abc"),
11+
("bbbbb", "b"),
12+
("pwwkew", "wke"),
13+
("", ""),
14+
("substring", "ubstring"),
15+
("eghghhgg", "egh"),
16+
("aabbcc", "ab"),
17+
("abccba", "abc"),
18+
]
19+
20+
21+
class LongestSubstringWithoutDuplicationTestCase(unittest.TestCase):
22+
@parameterized.expand(LONGEST_SUBSTRING_WITHOUT_REPEATING_CHARACTERS)
23+
def test_longest_substring_without_duplication(self, s: str, expected: str):
24+
actual = longest_substring_without_duplication(s)
25+
self.assertEqual(expected, actual)
26+
27+
28+
if __name__ == "__main__":
29+
unittest.main()

algorithms/sliding_window/longest_substring_without_repeating_characters/README.md

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

algorithms/sliding_window/longest_substring_without_repeating_characters/__init__.py

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

algorithms/sliding_window/longest_substring_without_repeating_characters/test_longest_substring_without_repeating_characters.py

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

0 commit comments

Comments
 (0)