-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFebruary-22.py
49 lines (42 loc) · 1013 Bytes
/
February-22.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
39
40
41
42
43
44
45
46
47
48
49
class Solution:
def maxLength(self, s):
l = r = m = 0
for c in s:
l += c == '('
r += c == ')'
if l == r:
m = max(m, 2 * r)
elif r > l:
l = r = 0
l = r = 0
for c in s[::-1]:
l += c == '('
r += c == ')'
if l == r:
m = max(m, 2 * l)
elif l > r:
l = r = 0
return m
2)
class Solution:
def maxLength(self, s):
st=[-1]; m=0
for i,c in enumerate(s):
if c=='(':
st.append(i)
else:
st.pop()
if not st: st.append(i)
else: m = max(m, i - st[-1])
return m
#{
# Driver Code Starts
# Initial Template for Python3
if __name__ == '__main__':
t = int(input())
for _ in range(t):
S = input()
ob = Solution()
print(ob.maxLength(S))
print("~")
# } Driver Code Ends