<p>A parentheses string is valid if and only if:</p>

<ul>
	<li>It is the empty string,</li>
	<li>It can be written as <code>AB</code> (<code>A</code> concatenated with <code>B</code>), where <code>A</code> and <code>B</code> are valid strings, or</li>
	<li>It can be written as <code>(A)</code>, where <code>A</code> is a valid string.</li>
</ul>

<p>You are given a parentheses string <code>s</code>. In one move, you can insert a parenthesis at any position of the string.</p>

<ul>
	<li>For example, if <code>s = &quot;()))&quot;</code>, you can insert an opening parenthesis to be <code>&quot;(<strong>(</strong>)))&quot;</code> or a closing parenthesis to be <code>&quot;())<strong>)</strong>)&quot;</code>.</li>
</ul>

<p>Return <em>the minimum number of moves required to make </em><code>s</code><em> valid</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;())&quot;
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(((&quot;
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s[i]</code> is either <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>.</li>
</ul>

// class Solution {
// public:
//     char Ostack[1000];
//     char Cstack[1000];
//     int Ot = -1 ;
//     int Ct = - 1 ;
//     int minAddToMakeValid(string s) {
//         int i = 0 ;

//         while ( i < s.size()){
//             char ch = s[i];

//             if ( ch == '('){
//                 Ostack[++Ot] = ch ;
//             }
//             else if ( ch == ')'){
//                 Cstack[++Ct] = ch ;
//             }
//             i++;
//         }
//         return ( Ct > Ot ) ? (Ct-Ot) : Ot - Ct ;
//     }
// };

class Solution {
public:
    char Rstack[1000];
    char Wstack[1000];
    int Rt = -1 ;
    int Wt = - 1 ;
    int minAddToMakeValid(string s) {
        int i = 0 ;

        while ( i < s.size()){
            char ch = s[i];

            if ( ch == '('){
                Rstack[++Rt] = ch ;
            } else if ( ch == ')' ){
                if ( Rt == -1 ){
                    Wt++ ;
                } else {
                    char c = Rstack[Rt];
                    if ( ch == ')' && c == '(' ){
                       Rt-- ;
                    }else {
                        Wt++ ;
                    }
                }
            }
            i++ ;
        }
        if ( Rt > -1){
            return (Rt+Wt) + 2 ;
        }
    return Wt + 1 ;
    }
};
8200