| 1 | class 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected