-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode-3.py
More file actions
47 lines (41 loc) · 1.19 KB
/
leetcode-3.py
File metadata and controls
47 lines (41 loc) · 1.19 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
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
if len(s) < 2:
return len(s)
result = 1
length = 2
while length <= len(s):
i = 1
hashmap = {}
goon = False
index = 0
while index < len(s):
c = s[index]
print(index, c, hashmap, length)
if not hashmap.get(c): # no duplicate item.
hashmap[c] = 1
if i == length:
result = length
length += 1 # Try longer one.
goon = True
break
else:
i += 1
index += 1
else:
#print("next jump:", index, i)
index = index - i + 2
i = 1
hashmap = {}
if not goon: # No longger one found.
break
return result
solution = Solution()
s = "abcabcbb"
#s = "aab"
#s = "dvdf"
print(solution.lengthOfLongestSubstring(s))