LeetCode Records - Question 921 Minimum Add to Make Parentheses Valid
class Solution {
public int minAddToMakeValid(String s) {
List<Character> list = new LinkedList<>();
for (char ch : s.toCharArray()) {
if (ch == ')' && !list.isEmpty() && list.getLast() == '(') {
list.removeLast();
} else {
list.addLast(ch);
}
}
return list.size();
}
}
- Runtime: 1 ms (Beats: 62.59%)
- Memory: 41.61 MB (Beats: 16.36%)