java: add «1963. Minimum Number of Swaps to Make the String Balanced»
URL: https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
1f69732126
commit
1b43a2fe51
1 changed files with 22 additions and 0 deletions
|
@ -0,0 +1,22 @@
|
|||
class Solution {
|
||||
public int minSwaps(String s) {
|
||||
var open = 0;
|
||||
for (var c : s.toCharArray()) {
|
||||
switch (c) {
|
||||
case '[':
|
||||
++open;
|
||||
break;
|
||||
case ']':
|
||||
if (open > 0) {
|
||||
--open;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
/* no-op */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return (1 + open) / 2;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue