-
Notifications
You must be signed in to change notification settings - Fork 0
/
48_max_length_of_nodup_str.py
38 lines (31 loc) · 1.13 KB
/
48_max_length_of_nodup_str.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
# -*- coding=utf-8 -*-
class Solution():
def get_max_length_of_undup_str(self, string):
if string is None or len(string) <= 1:
return int(string) if string is not None else None
res = [0 for i in range(len(string))]
index_arr = [-1 for i in range(52)]
res[0] = 1
index_arr[self.get_index(0, string)] = 0
for i in range(1, len(string)):
char_index = self.get_index(i, string)
delta = i - index_arr[char_index]
if index_arr[char_index] == -1 or delta > res[i-1]:
res[i] = res[i-1] + 1
else:
res[i] = delta
# 更新当前字符的最新索引
index_arr[char_index] = i
return res[-1]
def get_index(self, index, string):
temp = ord(string[index])
if 65 <= temp <= 90:
return temp - ord('A')
elif 97 <= temp <= 122:
return temp - ord('a') + 26
else:
raise TypeError('not a valid char!')
if __name__ == "__main__":
string = input()
ex = Solution()
print(ex.get_max_length_of_undup_str(string))