RainX_69

Minimum Insertions to Balance a SPECIAL Parentheses String | OA | MUST DO | TRICKY

Apr 6th, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | Source Code | 0 0
  1. https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/
  2.  
  3. Given a parentheses string s containing only the characters '(' and ')'. A parentheses string is balanced if:
  4. Any left parenthesis '(' must have a corresponding two consecutive right parenthesis '))'.
  5. Left parenthesis '(' must go before the corresponding two consecutive right parenthesis '))'.
  6. In other words, we treat '(' as an opening parenthesis and '))' as a closing parenthesis.
  7. For example, "())", "())(())))" and "(())())))" are balanced, ")()", "()))" and "(()))" are not balanced.
  8. You can insert the characters '(' and ')' at any position of the string to balance it if needed.
  9. Return the minimum number of insertions needed to make s balanced.
  10.  
  11. Example 1:
  12. Input: s = "(()))"
  13. Output: 1
  14. Explanation: The second '(' has two matching '))', but the first '(' has only ')' matching. We need to add one more ')' at the end of the string to be "(())))" which is balanced.
  15.  
  16. Example 2:
  17. Input: s = "())"
  18. Output: 0
  19. Explanation: The string is already balanced.
  20.  
  21. Example 3:
  22. Input: s = "))())("
  23. Output: 3
  24. Explanation: Add '(' to match the first '))', Add '))' to match the last '('.
  25.  
  26.  
  27. Constraints:
  28. 1 <= s.length <= 10^5
  29. s consists of '(' and ')' only.
  30.  
  31. ----------------------------------------------------------------------------------------------------------------------
  32.  
  33. class Solution {
  34. public:
  35.     int minInsertions(string s) {
  36.         int open=0;
  37.         int openNeeded=0;
  38.         int closeNeeded=0;
  39.         int n=s.size();
  40.         for(int i=0;i<n;i++){
  41.             if(s[i]=='('){
  42.                 open++;
  43.             }
  44.             else{
  45.                 if(i+1<n && s[i+1]==')'){
  46.                     if(open>0){ // if it can close an earlier opening
  47.                         open--;
  48.                     }
  49.                     else{ // you need one opening to add
  50.                         openNeeded++;
  51.                     }
  52.                     i++;
  53.                 }
  54.                 else if(open==0){ // if no open brackets are there to match and there is no ')' forward, we need both ) and (, example ")()", ")"
  55.                     closeNeeded++;
  56.                     openNeeded++;
  57.                 }
  58.                 else{ // example "()"
  59.                     open--;
  60.                     closeNeeded++;
  61.                 }
  62.             }
  63.         }
  64.         return 2*open+closeNeeded+openNeeded; // a single open takes 2 closing bracks
  65.     }
  66. };
Advertisement
Add Comment
Please, Sign In to add comment