-
Notifications
You must be signed in to change notification settings - Fork 43
/
longest-substring-without-repeating-characters.py
292 lines (257 loc) · 8 KB
/
longest-substring-without-repeating-characters.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
3. Longest Substring Without Repeating Characters
Medium
Given a string s, find the length of the longest substring without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints:
0 <= s.length <= 5 * 104
"""
# V0
# IDEA : brute force : SLIDING WINDOW + DICT
from collections import Counter
class Solution(object):
def lengthOfLongestSubstring(self, s):
if len(s) <=1:
return len(s)
_s = Counter()
_max = 0
tmp = 0
for i in range(len(s)):
for j in range(i, len(s)):
#print ("i, j = " + str(i) + ", " + str(j) + " _s = " + str(_s))
#print (s[j] not in _s)
if s[j] in _s:
_max = max(_max, tmp)
tmp = 0
### NOTE : we need to clear the Counter() (if s[j] in _s)
_s = Counter()
break
_s[s[j]] += 1
tmp += 1
return _max
# V0'
# IDEA : TWO POINTER + SLIDING WINDOW + DICT (NOTE this method !!!!)
# -> use a hash table (d) record visited "element" (e.g. : a,b,c,...)
# (but NOT sub-string)
class Solution(object):
def lengthOfLongestSubstring(self, s):
d = {}
# left pointer
l = 0
res = 0
# NOTE !!! right pointer
for r in range(len(s)):
"""
### NOTE : deal with "s[r] in d" case ONLY !!!
### NOTE : if already visited, means "repeating"
# -> then we need to update left pointer (l)
"""
if s[r] in d:
"""
NOTE !!! this
-> via max(l, d[s[r]] + 1) trick,
we can get the "latest" idx of duplicated s[r], and start from that one
"""
l = max(l, d[s[r]] + 1)
# if not visited yet, record the alphabet
# and re-calculate the max length
d[s[r]] = r
res = max(res, r -l + 1)
return res
# V0'
# IDEA : SLIDING WINDOW + defaultdict (brute force)
from collections import defaultdict
class Solution(object):
def lengthOfLongestSubstring(self, s):
if len(s) <= 1:
return len(s)
res = 0
tmp = defaultdict(int)
for i in range(len(s)):
for j in range(i, len(s)):
#print ("i = " + str(i) + " j = " + str(j))
if s[j] in tmp:
res = max(res, tmp[j]-i+1)
tmp = defaultdict(int)
break
else:
tmp[s[j]] = j
res = max(res, j-i+1)
return res
# V0''
# IDEA : GREEDY + 2 pointer + set
class Solution(object):
def lengthOfLongestSubstring(self, s):
left, right = 0, 0
chars = set()
res = 0
while left < len(s) and right < len(s):
if s[right] in chars:
if s[left] in chars:
chars.remove(s[left])
left += 1
else:
chars.add(s[right])
right += 1
res = max(res, len(chars))
return res
# V0'''
# IDEA : SLIDING WINDOW + DICT
class Solution(object):
def lengthOfLongestSubstring(self, s):
left, right = 0, 0
res = 0
chars = dict()
for right in range(len(s)):
if s[right] in chars:
left = max(left, chars[s[right]] + 1)
chars[s[right]] = right
res = max(res, right - left + 1)
return res
# V1
# IDEA : BRUTE FORCE (time out error)
# Time COMPLEXITY : O(n^3)
# Space COMPLEXITY : O(mim(n,m))
# https://leetcode.com/problems/longest-substring-without-repeating-characters/solution/
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
def check(start, end):
chars = [0] * 128
for i in range(start, end + 1):
c = s[i]
chars[ord(c)] += 1
if chars[ord(c)] > 1:
return False
return True
n = len(s)
res = 0
for i in range(n):
for j in range(i, n):
if check(i, j):
res = max(res, j - i + 1)
return res
# V1'
# IDEA : SLIDING WINDOW
# Time COMPLEXITY : O(n)
# Space COMPLEXITY : O(mim(n,m))
# https://leetcode.com/problems/longest-substring-without-repeating-characters/solution/
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
chars = [0] * 128
left = right = 0
res = 0
while right < len(s):
r = s[right]
chars[ord(r)] += 1
while chars[ord(r)] > 1:
l = s[left]
chars[ord(l)] -= 1
left += 1
res = max(res, right - left + 1)
right += 1
return res
# V1''
# IDEA : SLIDING WINDOW OPTIMIZED
# Time COMPLEXITY : O(n)
# Space COMPLEXITY : O(mim(n,m))
# https://leetcode.com/problems/longest-substring-without-repeating-characters/solution/
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
n = len(s)
ans = 0
# mp stores the current index of a character
mp = {}
i = 0
# try to extend the range [i, j]
for j in range(n):
if s[j] in mp:
i = max(mp[s[j]], i)
ans = max(ans, j - i + 1)
mp[s[j]] = j + 1
return ans
# V1'''
# https://blog.csdn.net/fuxuemingzhu/article/details/82022530
# IDEA : GREEDY
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
left, right = 0, 0
chars = set()
res = 0
while left < len(s) and right < len(s):
if s[right] in chars:
if s[left] in chars:
chars.remove(s[left])
left += 1
else:
chars.add(s[right])
right += 1
res = max(res, len(chars))
return res
# V1'''''
# http://bookshadow.com/weblog/2015/04/05/leetcode-longest-substring-without-repeating-characters/
# IDEA : MOVING WINDOW + DICT
class Solution:
# @return an integer
def lengthOfLongestSubstring(self, s):
ans, start, end = 0, 0, 0
countDict = {}
for c in s:
end += 1
countDict[c] = countDict.get(c, 0) + 1
while countDict[c] > 1:
countDict[s[start]] -= 1
start += 1
ans = max(ans, end - start)
return ans
# V1'''''
# https://www.jiuzhang.com/solution/longest-substring-without-repeating-characters/#tag-highlight-lang-python
class Solution:
"""
@param s: a string
@return: an integer
"""
def lengthOfLongestSubstring(self, s):
unique_chars = set([])
j = 0
n = len(s)
longest = 0
for i in range(n):
while j < n and s[j] not in unique_chars:
unique_chars.add(s[j])
j += 1
longest = max(longest, j - i)
unique_chars.remove(s[i])
return longest
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
# @return an integer
def lengthOfLongestSubstring(self, s):
longest, start, visited = 0, 0, [False for _ in range(256)]
for i, char in enumerate(s):
if visited[ord(char)]:
while char != s[start]:
visited[ord(s[start])] = False
start += 1
start += 1
else:
visited[ord(char)] = True
longest = max(longest, i - start + 1)
return longest