-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBalanced Subsequence.py
More file actions
45 lines (35 loc) · 907 Bytes
/
Balanced Subsequence.py
File metadata and controls
45 lines (35 loc) · 907 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Sears Dots and Arrow
Balanced Subsequence
All Test cases passed
"""
def getBalancedSubSequence(S):
stack = []
i = 0
while(S[i] == ')'):
i = i + 1
#Now I have the first place where S[i] = '('
if ( i == len(S) - 1):
return 0
result = 0
for item in S[i:]:
if (item == '('):
stack.append(item)
elif (item == ')'):
if (len(stack) > 0 and stack.pop(-1) == '('):
result = result + 2 # becuase each () will count for 2 rather than 1 balanced pair
#Else ignore this
return result
def inputs():
SList = []
T = int(raw_input(''))
for i in range(T):
S = raw_input('')
SList.append(S)
return SList
if __name__ =="__main__":
SList = inputs()
#['()())', '))))(((', '()(((((()']
#
for S in SList:
print getBalancedSubSequence(S)