<p>Given a string <font face="monospace">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p>

<p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>, in any positions ) so that the resulting <em>parentheses string</em> is valid and return <strong>any</strong> valid string.</p>

<p>Formally, a <em>parentheses string</em> is valid if and only if:</p>

<ul>
	<li>It is the empty string, contains only lowercase characters, or</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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;lee(t(c)o)de)&quot;
<strong>Output:</strong> &quot;lee(t(c)o)de&quot;
<strong>Explanation:</strong> &quot;lee(t(co)de)&quot; , &quot;lee(t(c)ode)&quot; would also be accepted.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;a)b(c)d&quot;
<strong>Output:</strong> &quot;ab(c)d&quot;
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;))((&quot;
<strong>Output:</strong> &quot;&quot;
<strong>Explanation:</strong> An empty string is also valid.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s[i]</code> is either&nbsp;<code>&#39;(&#39;</code> , <code>&#39;)&#39;</code>, or lowercase English letter.</li>
</ul>

class Solution {
public:
    string minRemoveToMakeValid(string s) {
      vector<int> pl;
              vector<int> pr;

                      for(int i=0;i<s.length();i++){
                                  if(s[i]=='('){
                                                  pl.push_back(i);
                                                              }
                                                                          else if(s[i]==')'){
                                                                                          pr.push_back(i);
                                                                                                      }
                                                                                                              }


                                                                                                                              int p1=0,r1=0,c=0;//c is to change the index

                                                                                                                                      while(p1<pl.size() && r1<pr.size()){
                                                                                                                                                  if(pl[p1]>pr[r1]){
                                                                                                                                                                  s.erase(s.begin()+pr[r1]-c);
                                                                                                                                                                                  c++;
                                                                                                                                                                                                  r1++;
                                                                                                                                                                                                              }
                                                                                                                                                                                                                          else{
                                                                                                                                                                                                                                          p1++;
                                                                                                                                                                                                                                                          r1++;
                                                                                                                                                                                                                                                                      }
                                                                                                                                                                                                                                                                              }
                                                                                                                                                                                                                                                                                      //if the pointers are not reached to the last,then there are extra brackets,so erase them
                                                                                                                                                                                                                                                                                              while(p1<pl.size()){
                                                                                                                                                                                                                                                                                                          s.erase(s.begin()+pl[p1]-c);
                                                                                                                                                                                                                                                                                                                      c++;
                                                                                                                                                                                                                                                                                                                                  p1++;
                                                                                                                                                                                                                                                                                                                                          }
                                                                                                                                                                                                                                                                                                                                                  while(r1<pr.size()){
                                                                                                                                                                                                                                                                                                                                                              s.erase(s.begin()+pr[r1]-c);
                                                                                                                                                                                                                                                                                                                                                                          c++;
                                                                                                                                                                                                                                                                                                                                                                                      r1++;
                                                                                                                                                                                                                                                                                                                                                                                              }

                                                                                                                                                                                                                                                                                                                                                                                                      return s;  
    }
};
16400