forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum_sum_subarray.py
More file actions
61 lines (46 loc) · 1.81 KB
/
maximum_sum_subarray.py
File metadata and controls
61 lines (46 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# created by Mayank
"""
Sliding Window Algorithm: Longest Substring Without Repeating Characters
https://en.wikipedia.org/wiki/Sliding_window
This algorithm finds the length of the longest substring within a given string
that does not contain any repeating characters. It uses a "sliding window"
approach with two pointers to efficiently track the current substring.
Complexity: O(n) where n is the length of the string.
For doctests run following command:
python3 -m doctest -v longest_substring_without_repeating_characters.py
"""
def longest_substring_without_repeating_characters(s: str) -> int:
"""
Finds the length of the longest substring without repeating characters.
:param s: The input string.
:return: The length of the longest substring.
Examples:
>>> longest_substring_without_repeating_characters("abcabcbb")
3
>>> longest_substring_without_repeating_characters("bbbbb")
1
>>> longest_substring_without_repeating_characters("pwwkew")
3
>>> longest_substring_without_repeating_characters("")
0
>>> longest_substring_without_repeating_characters("abcdef")
6
>>> longest_substring_without_repeating_characters("tmmzuxt")
5
"""
char_set: set[str] = set()
left_pointer = 0
max_length = 0
for right_pointer in range(len(s)):
# If the character is already in the set, shrink the window from the left
while s[right_pointer] in char_set:
char_set.remove(s[left_pointer])
left_pointer += 1
# Add the new character to the set (expanding the window)
char_set.add(s[right_pointer])
# Update the maximum length found so far
max_length = max(max_length, right_pointer - left_pointer + 1)
return max_length
if __name__ == "__main__":
import doctest
doctest.testmod()