-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLengthOfLongestSubstring.py
More file actions
32 lines (30 loc) · 984 Bytes
/
LengthOfLongestSubstring.py
File metadata and controls
32 lines (30 loc) · 984 Bytes
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
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
allLen = 0
subStr = []
for curIndex in range(len(s)):
cha = s[curIndex]
if cha in subStr:
lastChaIndex = subStr.index(cha)
preLen = lastChaIndex + 1
nextLen = len(subStr) - preLen -1
if(allLen > max([preLen,nextLen])):
subStr = []
subStr.append(cha)
elif preLen > nextLen :
allLen = preLen
subStr = subStr[lastChaIndex + 1:]
else:
subStr = subStr[lastChaIndex + 1:]
allLen = nextLen
else:
subStr.append(cha)
if allLen == 0:
allLen = 1
return allLen
s = Solution()
print s.lengthOfLongestSubstring("pwwkew")