1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-10-18 14:52:09 +02:00
LeetCode/java/minimum-add-to-make-parentheses-valid.java

25 lines
436 B
Java

class Solution {
public int minAddToMakeValid(String s) {
int open = 0, needed = 0;
for (var c : s.toCharArray()) {
switch (c) {
case '(':
++open;
break;
case ')':
if (open > 0) {
--open;
} else {
++needed;
}
break;
default:
/* no-op */
break;
}
}
return open + needed;
}
}