nikunjsoni

921

Mar 22nd, 2021
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     int minAddToMakeValid(string S) {
  4.         int open, ans;
  5.         open = ans = 0;
  6.         for(char c: S){
  7.             if(c == '(') open++;
  8.             if(c == ')' && !open) ans++;
  9.             if(c == ')' && open) open--;
  10.         }
  11.         ans += open;
  12.         return ans;
  13.     }
  14. };
RAW Paste Data