MCPcopy Index your code
hub / github.com/Tiwarishashwat/InterviewCodes / minAddToMakeValid

Method minAddToMakeValid

MinAddToMakeStringValid.java:3–18  ·  view source on GitHub ↗
(String s)

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

Callers

nothing calls this directly

Calls 3

pushMethod · 0.80
popMethod · 0.80
isEmptyMethod · 0.45

Tested by

no test coverage detected