Matej Focko
f57f51a287
URL: https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/ Signed-off-by: Matej Focko <me@mfocko.xyz>
25 lines
436 B
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;
|
|
}
|
|
}
|