Matej Focko
1b43a2fe51
URL: https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/ Signed-off-by: Matej Focko <me@mfocko.xyz>
22 lines
374 B
Java
22 lines
374 B
Java
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;
|
|
}
|
|
}
|