-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-substring-without-repeating-characters-3.py
51 lines (43 loc) · 1.38 KB
/
longest-substring-without-repeating-characters-3.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
# Sol 1 - using dictionary
def lengthOfLongestSubstring(self, s: str) -> int:
res = 0
slow = 0
seen = {}
for fast in range(len(s)):
if s[fast] in seen and slow <= seen[s[fast]]:
slow = seen[s[fast]] + 1
else:
res = max(res, fast - slow + 1)
seen[s[fast]] = fast
return res
# Sol 2 - using set
def lengthOfLongestSubstring(self, s: str) -> int:
seen = set()
res = 0
slow = 0
for fast in range(len(s)):
while s[fast] in seen:
seen.remove(s[slow])
slow += 1
seen.add(s[fast])
res = max(res, fast - slow + 1)
return res
# Sol 3 - dictionary
def lengthOfLongestSubstring(self, s: str) -> int:
res = 0
slow = 0
seen = {}
for fast in range(len(s)):
# if s[fast] not in seen, keep increasing window size & move right
if s[fast] not in seen:
res = max(res, fast - slow + 1)
# if s[fast] in seen, we have two cases:
else:
# case 1: s[fast] is inside the current window, change window by moving slow to seen[s[fast]] + 1
# case 2: s[fast] is not inside the current window, increase window
if seen[s[fast]] < slow:
res = max(res, fast - slow + 1)
else:
slow = seen[s[fast]] + 1
seen[s[fast]] = fast
return res