|
| 1 | +// Source : https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2020-10-02 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * Given a parentheses string s containing only the characters '(' and ')'. A parentheses string is |
| 8 | + * balanced if: |
| 9 | + * |
| 10 | + * Any left parenthesis '(' must have a corresponding two consecutive right parenthesis '))'. |
| 11 | + * Left parenthesis '(' must go before the corresponding two consecutive right parenthesis |
| 12 | + * '))'. |
| 13 | + * |
| 14 | + * In other words, we treat '(' as openning parenthesis and '))' as closing parenthesis. |
| 15 | + * |
| 16 | + * For example, "())", "())(())))" and "(())())))" are balanced, ")()", "()))" and "(()))" are not |
| 17 | + * balanced. |
| 18 | + * |
| 19 | + * You can insert the characters '(' and ')' at any position of the string to balance it if needed. |
| 20 | + * |
| 21 | + * Return the minimum number of insertions needed to make s balanced. |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * |
| 25 | + * Input: s = "(()))" |
| 26 | + * Output: 1 |
| 27 | + * Explanation: The second '(' has two matching '))', but the first '(' has only ')' matching. We need |
| 28 | + * to to add one more ')' at the end of the string to be "(())))" which is balanced. |
| 29 | + * |
| 30 | + * Example 2: |
| 31 | + * |
| 32 | + * Input: s = "())" |
| 33 | + * Output: 0 |
| 34 | + * Explanation: The string is already balanced. |
| 35 | + * |
| 36 | + * Example 3: |
| 37 | + * |
| 38 | + * Input: s = "))())(" |
| 39 | + * Output: 3 |
| 40 | + * Explanation: Add '(' to match the first '))', Add '))' to match the last '('. |
| 41 | + * |
| 42 | + * Example 4: |
| 43 | + * |
| 44 | + * Input: s = "((((((" |
| 45 | + * Output: 12 |
| 46 | + * Explanation: Add 12 ')' to balance the string. |
| 47 | + * |
| 48 | + * Example 5: |
| 49 | + * |
| 50 | + * Input: s = ")))))))" |
| 51 | + * Output: 5 |
| 52 | + * Explanation: Add 4 '(' at the beginning of the string and one ')' at the end. The string becomes |
| 53 | + * "(((())))))))". |
| 54 | + * |
| 55 | + * Constraints: |
| 56 | + * |
| 57 | + * 1 <= s.length <= 10^5 |
| 58 | + * s consists of '(' and ')' only. |
| 59 | + ******************************************************************************************************/ |
| 60 | +class Solution { |
| 61 | +public: |
| 62 | + int minInsertions(string s) { |
| 63 | + vector<char> stack; |
| 64 | + |
| 65 | + int cnt = 0; |
| 66 | + int len = s.size(); |
| 67 | + for (int i=0; i<len; i++) { |
| 68 | + |
| 69 | + if ( s[i] == '(' ) { |
| 70 | + stack.push_back( s[i] ); |
| 71 | + continue; |
| 72 | + } |
| 73 | + // if s[i] is ')' |
| 74 | + if (stack.size() > 0) { |
| 75 | + stack.pop_back(); |
| 76 | + } else { |
| 77 | + cnt++; // missed the '(' |
| 78 | + } |
| 79 | + |
| 80 | + // if s[i+1] is ')', need to skip |
| 81 | + if ( i < len -1 && s[i+1] == ')' ) { |
| 82 | + i++; |
| 83 | + }else{ |
| 84 | + cnt++; //missed the ')' |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + // if the stack still has '(', which means need double of ')' |
| 90 | + return cnt + stack.size()*2; |
| 91 | + } |
| 92 | +}; |
0 commit comments