Skip to content

Commit 68725a2

Browse files
authored
Create find-longest-awesome-substring.py
1 parent 680b10e commit 68725a2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(10 * n)
2+
# Space: O(1024)
3+
4+
class Solution(object):
5+
def longestAwesome(self, s):
6+
"""
7+
:type s: str
8+
:rtype: int
9+
"""
10+
ALPHA_BET_SIZE = 10
11+
result, mask, lookup = 0, 0, [len(s)]*(2**ALPHA_BET_SIZE)
12+
lookup[0] = -1
13+
for i, ch in enumerate(s):
14+
mask ^= 2**(ord(ch)-ord('0'))
15+
if lookup[mask] == len(s):
16+
lookup[mask] = i
17+
result = max(result, i - lookup[mask])
18+
for d in xrange(ALPHA_BET_SIZE):
19+
result = max(result, i - lookup[mask^(2**d)])
20+
return result

0 commit comments

Comments
 (0)