Advertisement
abhyash

DKL

Nov 2nd, 2023
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.43 KB | None | 0 0
  1. class Solution {
  2.     public int minAddToMakeValid(String s) {
  3.         Stack<Character>stack = new Stack<>();
  4.  
  5.         for(char c : s.toCharArray()){
  6.             if(c=='('){
  7.                 stack.push(c);
  8.             }else if(c == ')' && !stack.isEmpty() && stack.peek()=='('){
  9.                 stack.pop();
  10.             }else{
  11.                 stack.push(c);
  12.             }
  13.         }
  14.         return stack.size();
  15.        
  16.     }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement