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

Class Solution

MinimumAddToMakeParenthesisValid.java:1–24  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2 public int minAddToMakeValid(String s) {
3
4 int open=0,close=0;
5// look only for invalid pairs
6
7 for(int i=0;i<s.length();i++)
8 {
9 // open the brackets whenever possible
10 if(s.charAt(i)=='(') open++;
11 else
12 {
13 // if its not possible to make a pair then add the closing ones
14 if(open==0) close++;
15 // closing the earlier opened brackets
16 else open--;
17 }
18 }
19 // for making each invalid bracket a valid one, we need to include its opposite one
20 return(open+close);
21
22
23 }
24}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected