forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_non_repeat.py
More file actions
149 lines (120 loc) · 4.11 KB
/
longest_non_repeat.py
File metadata and controls
149 lines (120 loc) · 4.11 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating
characters. Multiple algorithm variants are provided.
Reference: https://leetcode.com/problems/longest-substring-without-repeating-characters/
Complexity:
Time: O(n) for all variants
Space: O(min(n, m)) where m is the charset size
"""
from __future__ import annotations
def longest_non_repeat_v1(string: str) -> int:
"""Find the length of the longest substring without repeating characters.
Args:
string: Input string to search.
Returns:
Length of the longest non-repeating substring.
Examples:
>>> longest_non_repeat_v1("abcabcbb")
3
"""
if string is None:
return 0
char_index = {}
max_length = 0
start = 0
for index in range(len(string)):
if string[index] in char_index:
start = max(char_index[string[index]], start)
char_index[string[index]] = index + 1
max_length = max(max_length, index - start + 1)
return max_length
def longest_non_repeat_v2(string: str) -> int:
"""Find the length of the longest substring without repeating characters.
Args:
string: Input string to search.
Returns:
Length of the longest non-repeating substring.
Examples:
>>> longest_non_repeat_v2("abcabcbb")
3
"""
if string is None:
return 0
start, max_length = 0, 0
used_char = {}
for index, char in enumerate(string):
if char in used_char and start <= used_char[char]:
start = used_char[char] + 1
else:
max_length = max(max_length, index - start + 1)
used_char[char] = index
return max_length
def get_longest_non_repeat_v1(string: str) -> tuple[int, str]:
"""Find the longest substring without repeating characters.
Args:
string: Input string to search.
Returns:
A tuple of (length, substring) for the longest non-repeating substring.
Examples:
>>> get_longest_non_repeat_v1("abcabcbb")
(3, 'abc')
"""
if string is None:
return 0, ""
substring = ""
char_index = {}
max_length = 0
start = 0
for index in range(len(string)):
if string[index] in char_index:
start = max(char_index[string[index]], start)
char_index[string[index]] = index + 1
if index - start + 1 > max_length:
max_length = index - start + 1
substring = string[start : index + 1]
return max_length, substring
def get_longest_non_repeat_v2(string: str) -> tuple[int, str]:
"""Find the longest substring without repeating characters.
Args:
string: Input string to search.
Returns:
A tuple of (length, substring) for the longest non-repeating substring.
Examples:
>>> get_longest_non_repeat_v2("abcabcbb")
(3, 'abc')
"""
if string is None:
return 0, ""
substring = ""
start, max_length = 0, 0
used_char = {}
for index, char in enumerate(string):
if char in used_char and start <= used_char[char]:
start = used_char[char] + 1
else:
if index - start + 1 > max_length:
max_length = index - start + 1
substring = string[start : index + 1]
used_char[char] = index
return max_length, substring
def get_longest_non_repeat_v3(string: str) -> tuple[int, str]:
"""Find the longest substring without repeating characters using sliding window.
Args:
string: Input string to search.
Returns:
A tuple of (length, substring) for the longest non-repeating substring.
Examples:
>>> get_longest_non_repeat_v3("abcabcbb")
(3, 'abc')
"""
longest_substring = ""
seen = set()
start_index = 0
for i in range(len(string)):
while string[i] in seen:
seen.remove(string[start_index])
start_index += 1
seen.add(string[i])
longest_substring = max(longest_substring, string[start_index : i + 1], key=len)
return len(longest_substring), longest_substring