-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq1542.py
More file actions
29 lines (28 loc) · 1023 Bytes
/
q1542.py
File metadata and controls
29 lines (28 loc) · 1023 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
class Solution:
def longestAwesome(self, s: str) -> int:
result = 1
len_s = len(s)
dic = {0:-1}
number = 0
for i in range(len_s):
char = s[i]
number ^= 1 << int(char)
if number not in dic:
dic[number] = i
# 如果发现两个一样的key,则i - dic[number]期间的数全部重复,全部重复,可以再随意加一个数
elif i - dic[number] + 1 > result:
result = i - dic[number] + 1
for j in range(10):
if int(char) == j:
continue
# 如果只差一个数,将这个数异或掉
temp = (1 << j) ^ number
if temp in dic and result < i - dic[temp]:
result = i - dic[temp]
# i - dic[number] + 1 可以大于 len_s
if result > len_s:
result = len_s
return result
s = "78133156"
result = Solution().longestAwesome(s)
print(result)