Advertisement
nikunjsoni

1541

Apr 3rd, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minInsertions(string s) {
  4.         int open, ans;
  5.         open = ans = 0;
  6.         for(int i=0; i<s.length(); i++){
  7.             if(s[i] ==  '(') open++;
  8.             if(s[i] == ')'){
  9.                 if(i+1 < s.length() && s[i+1] == ')') i++;
  10.                 else ans++;
  11.                 if(open>0)open--;
  12.                 else ans++;
  13.             }
  14.         }
  15.         return ans + (open*2);
  16.     }
  17. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement