Skip to content

Commit f5bf87b

Browse files
authored
Merge pull request #3673 from NotADucc/1963
create 1963-minimum-number-of-swaps-to-make-the-string-balanced.cs
2 parents f935e60 + caa375e commit f5bf87b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Solution
2+
{
3+
public int MinSwaps(string s)
4+
{
5+
int open_braces = 0, swaps = 0;
6+
foreach (var ch in s)
7+
{
8+
if (ch == '[')
9+
{
10+
open_braces++;
11+
}
12+
else
13+
{
14+
if (open_braces <= 0)
15+
{
16+
open_braces++;
17+
swaps++;
18+
}
19+
else
20+
{
21+
open_braces--;
22+
}
23+
}
24+
}
25+
26+
return swaps;
27+
}
28+
}

0 commit comments

Comments
 (0)