-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathlongest-balanced-substring-ii.py
More file actions
60 lines (54 loc) · 1.68 KB
/
longest-balanced-substring-ii.py
File metadata and controls
60 lines (54 loc) · 1.68 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Time: O(n)
# Space: O(n)
import collections
# hash table, prefix sum
class Solution(object):
def longestBalanced(self, s):
"""
:type s: str
:rtype: int
"""
def count1():
result = cnt = 0
for i in xrange(len(s)):
cnt += 1
if i+1 == len(s) or s[i+1] != s[i]:
result = max(result, cnt)
cnt = 0
return result
def count2(a, b):
result = cnt = 0
lookup = collections.defaultdict(int, {cnt:-1})
for i, x in enumerate(s):
if x == a:
cnt += 1
elif x == b:
cnt -= 1
else:
cnt = 0
lookup = collections.defaultdict(int, {cnt:i})
continue
if cnt in lookup:
result = max(result, i-lookup[cnt])
else:
lookup[cnt] = i
return result
def count3():
result = a = b = 0
lookup = collections.defaultdict(int, {(a, b):-1})
for i, x in enumerate(s):
if x == 'a':
a += 1
elif x == 'b':
b += 1
else:
a -= 1
b -= 1
if (a, b) in lookup:
result = max(result, i-lookup[a, b])
else:
lookup[a, b] = i
return result
return max(count1(),
count2('a', 'b'), count2('b', 'c'), count2('c', 'a'),
count3())