-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03.py
More file actions
37 lines (35 loc) · 1.45 KB
/
03.py
File metadata and controls
37 lines (35 loc) · 1.45 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
__author__ = 'alex'
class Solution(object):
def lengthOfLongestSubstring(self, s):
if s == "":
return 0
posi_dict = dict()
start_position = 0
current_longest_length = 0
posi = 0
for index in range(len(s)):
if not s[index] in posi_dict:
posi_dict[s[index]] = index
if index == len(s) - 1:
if index - start_position + 1 > current_longest_length:
print start_position
return index - start_position + 1
else:
if start_position == posi_dict[s[index]]:
current_length = index - start_position
if current_length > current_longest_length:
current_longest_length = current_length
posi = start_position
else:
current_length = index - start_position
if current_length > current_longest_length:
current_longest_length = current_length
posi = start_position
start_position = posi_dict[s[index]] + 1
posi_dict.clear()
for i in range(start_position, index+1):
posi_dict[s[i]] = i
print "last: %s" % posi
return current_longest_length
solution = Solution()
print solution.lengthOfLongestSubstring("db!@#$%^&acbvv")