Skip to content

Commit 8ba1467

Browse files
committed
balance parenthesis
1 parent f0b659f commit 8ba1467

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

DSA Crack Sheet/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
- [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/ "view question") - [Cpp Solution](./solutions/Longest%20Common%20Prefix.cpp)
8383
- [Min Number of Flips](https://practice.geeksforgeeks.org/problems/min-number-of-flips/0# "view question") - [Cpp Solution](./solutions/Min%20Number%20of%20Flips.cpp)
8484
- [Second most repeated string in a sequence](https://practice.geeksforgeeks.org/problems/second-most-repeated-string-in-a-sequence0534/1# "view question") - [Cpp Solution](./solutions/Second%20most%20repeated%20string%20in%20a%20sequence.cpp)
85+
- [Minimum Swaps for Bracket Balancing](https://practice.geeksforgeeks.org/problems/minimum-swaps-for-bracket-balancing/0# "view question") - [Cpp Solution](./solutions/Minimum%20Swaps%20for%20Bracket%20Balancing.cpp)
8586
- []( "view question") - [Cpp Solution](./solutions/.cpp)
8687

8788
### Searching & Sorting
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Minimum Swaps for Bracket Balancing
3+
===================================
4+
5+
You are given a string of 2N characters consisting of N ‘[‘ brackets and N ‘]’ brackets. A string is considered balanced if it can be represented in the for S2[S1] where S1 and S2 are balanced strings. We can make an unbalanced string balanced by swapping adjacent characters. Calculate the minimum number of swaps necessary to make a string balanced.
6+
7+
Input:
8+
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains an integer N denoting the length of the string.
9+
10+
The second line of each test case contains the string consisting of '[' and ']'.
11+
12+
Output:
13+
Print the minimum number of swaps to make the string balanced for each test case in a new line.
14+
15+
Constraints:
16+
1<= T <=100
17+
1<= N <=100000
18+
19+
Example:
20+
21+
Input : []][][
22+
Output : 2
23+
First swap: Position 3 and 4
24+
[][]][
25+
Second swap: Position 5 and 6
26+
[][][]
27+
28+
Input : [[][]]
29+
Output : 0
30+
String is already balanced.
31+
*/
32+
33+
#include <bits/stdc++.h>
34+
using namespace std;
35+
36+
int main()
37+
{
38+
int t;
39+
cin >> t;
40+
while (t--)
41+
{
42+
int n;
43+
cin >> n;
44+
string s;
45+
cin >> s;
46+
47+
vector<int> pos;
48+
for (int i = 0; i < s.length(); ++i)
49+
if (s[i] == '[')
50+
pos.push_back(i);
51+
52+
int count = 0;
53+
int p = 0;
54+
long sum = 0;
55+
56+
for (int i = 0; i < s.length(); ++i)
57+
{
58+
if (s[i] == '[')
59+
{
60+
++count;
61+
++p;
62+
}
63+
else if (s[i] == ']')
64+
--count;
65+
66+
if (count < 0)
67+
{
68+
sum += pos[p] - i;
69+
swap(s[i], s[pos[p]]);
70+
++p;
71+
count = 1;
72+
}
73+
}
74+
cout << sum << endl;
75+
}
76+
return 0;
77+
}

0 commit comments

Comments
 (0)