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:
parent
1b43a2fe51
commit
f57f51a287
1 changed files with 25 additions and 0 deletions
25
java/minimum-add-to-make-parentheses-valid.java
Normal file
25
java/minimum-add-to-make-parentheses-valid.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue