java: add «921. Minimum Add to Make Parentheses Valid»

URL:	https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-10-09 09:20:27 +02:00
parent 1b43a2fe51
commit f57f51a287
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,25 @@
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;
}
}