-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLongestSubStringWORepeatingChars.py
50 lines (45 loc) · 1.54 KB
/
LongestSubStringWORepeatingChars.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
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
if len(s) < 2:
return len(s)
i = 0
j = 1
counts = {}
counts[s[i]] = 1
last_is_good = True
mx = 1, ""
while True:
if last_is_good and j < len(s):
j += 1
if counts.has_key(s[j-1]):
# If adding j-1th element keeps the substring distinct
if counts[s[j-1]] == 0:
if len(s[i:j]) > mx[0]:
mx = len(s[i:j]), s[i:j]
counts[s[j-1]] = 1
# If adding j-1th element causes a duplicate character
else:
last_is_good = False
counts[s[j-1]] += 1
# If adding j-1th element keeps the substring distinct
else:
if len(s[i:j]) > mx[0]:
mx = len(s[i:j]), s[i:j]
counts[s[j-1]] = 1
else:
while s[i] != s[j-1]:
counts[s[i]] -= 1
i += 1
counts[s[i]] -= 1
i += 1
last_is_good = True
# print counts
# print i, j, last_is_good
if j == len(s) and last_is_good:
break
# print mx
return mx[0]