-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_the_reversal.py
63 lines (45 loc) · 1.49 KB
/
count_the_reversal.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Given a string S consisting of only opening and closing curly brackets '{' and '}', find out the minimum number of reversals required to convert the string into a balanced expression.
A reversal means changing '{' to '}' or vice-versa.
Example 1:
Input:
S = "}{{}}{{{"
Output: 3
Explanation: One way to balance is:
"{{{}}{}}". There is no balanced sequence
that can be formed in lesser reversals.
Example 2:
Input:
S = "{{}{{{}{{}}{{"
Output: -1
Explanation: There's no way we can balance
this sequence of braces.
Your Task:
You don't need to read input or print anything. Your task is to complete the function countRev() which takes the string S as the input parameter and returns the minimum number of reversals required to balance the bracket sequence. If balancing is not possible, return -1.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ |S| ≤ 105
"""
def countRev (s):
# your code here
if len(s) % 2 != 0:
return -1
open_count, close_count = 0, 0
for char in s:
if char == '{':
open_count = open_count + 1
else:
# char == '}'
if open_count > 0:
open_count = open_count - 1
close_count = close_count + 1
return (open_count + close_count) // 2
#{
# Driver Code Starts
t = int (input ())
for tc in range (t):
s = input ()
print (countRev (s))
# Contributed By: Pranay Bansal
# } Driver Code Ends