MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Solution

Class Solution

MinAddToMakeStringValid.java:2–19  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1//stack approach
2class Solution {
3public int minAddToMakeValid(String s) {
4 Stack<Character> stack = new Stack<>();
5 for(int i=0;i<s.length();i++){
6 char ch = s.charAt(i);
7 if(ch == '('){
8 stack.push(ch);
9 } else {
10 if(stack.isEmpty() || stack.peek()==')') {
11 stack.push(ch);
12 } else {
13 stack.pop();
14 }
15 }
16 }
17 return stack.size();
18 }
19}
20
21//two pointers
22class Solution {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected