-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3673 from NotADucc/1963
create 1963-minimum-number-of-swaps-to-make-the-string-balanced.cs
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
csharp/1963-minimum-number-of-swaps-to-make-the-string-balanced.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
public class Solution | ||
{ | ||
public int MinSwaps(string s) | ||
{ | ||
int open_braces = 0, swaps = 0; | ||
foreach (var ch in s) | ||
{ | ||
if (ch == '[') | ||
{ | ||
open_braces++; | ||
} | ||
else | ||
{ | ||
if (open_braces <= 0) | ||
{ | ||
open_braces++; | ||
swaps++; | ||
} | ||
else | ||
{ | ||
open_braces--; | ||
} | ||
} | ||
} | ||
|
||
return swaps; | ||
} | ||
} |