{"id":5809,"date":"2019-11-09T08:36:13","date_gmt":"2019-11-09T16:36:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5809"},"modified":"2019-11-09T08:36:25","modified_gmt":"2019-11-09T16:36:25","slug":"1249-minimum-remove-to-make-valid-parentheses","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/1249-minimum-remove-to-make-valid-parentheses\/","title":{"rendered":"\u82b1\u82b1\u9171 1249. Minimum Remove to Make Valid Parentheses"},"content":{"rendered":"\n<p>Given a string&nbsp;s&nbsp;of&nbsp;<code>'('<\/code>&nbsp;,&nbsp;<code>')'<\/code>&nbsp;and lowercase English characters.&nbsp;<\/p>\n\n\n\n<p>Your task is to remove the minimum number of parentheses (&nbsp;<code>'('<\/code>&nbsp;or&nbsp;<code>')'<\/code>,&nbsp;in any positions ) so that the resulting&nbsp;<em>parentheses string<\/em>&nbsp;is valid and return&nbsp;<strong>any<\/strong>&nbsp;valid string.<\/p>\n\n\n\n<p>Formally, a&nbsp;<em>parentheses string<\/em>&nbsp;is valid if and only if:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It is the empty string, contains only lowercase characters, or<\/li><li>It can be written as&nbsp;<code>AB<\/code>&nbsp;(<code>A<\/code>&nbsp;concatenated with&nbsp;<code>B<\/code>), where&nbsp;<code>A<\/code>&nbsp;and&nbsp;<code>B<\/code>&nbsp;are valid strings, or<\/li><li>It can be written as&nbsp;<code>(A)<\/code>, where&nbsp;<code>A<\/code>&nbsp;is a valid string.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"lee(t(c)o)de)\"\n<strong>Output:<\/strong> \"lee(t(c)o)de\"\n<strong>Explanation:<\/strong> \"lee(t(co)de)\" , \"lee(t(c)ode)\" would also be accepted.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"a)b(c)d\"\n<strong>Output:<\/strong> \"ab(c)d\"\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"))((\"\n<strong>Output:<\/strong> \"\"\n<strong>Explanation:<\/strong> An empty string is also valid.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"(a(b(c)d)\"\n<strong>Output:<\/strong> \"a(b(c)d)\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 10^5<\/code><\/li><li><code>s[i]<\/code>&nbsp;is one&nbsp;of&nbsp;&nbsp;<code>'('<\/code>&nbsp;,&nbsp;<code>')'<\/code>&nbsp;and&nbsp;lowercase English letters<code>.<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Couting<\/strong><\/h2>\n\n\n\n<p>Count how many &#8220;(&#8221; are open and how many &#8220;)&#8221; left.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  string minRemoveToMakeValid(string s) {    \n    int close = count(begin(s), end(s), ')');    \n    int open = 0;\n    string ans;    \n    \n    for (char c : s) {\n      if (c == '(') {\n        if (open == close) continue;\n        ++open;\n      } else if (c == ')') {\n        --close;\n        if (open == 0) continue;\n        --open;\n      }\n      ans += c;\n    }    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;of&nbsp;&#8216;(&#8216;&nbsp;,&nbsp;&#8216;)&#8217;&nbsp;and lowercase English characters.&nbsp; Your task is to remove the minimum number of parentheses (&nbsp;&#8216;(&#8216;&nbsp;or&nbsp;&#8216;)&#8217;,&nbsp;in any positions ) so that the resulting&nbsp;parentheses string&nbsp;is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[177,421,180,4],"class_list":["post-5809","post","type-post","status-publish","format-standard","hentry","category-string","tag-medium","tag-parentheses","tag-stack","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5809"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5809\/revisions"}],"predecessor-version":[{"id":5811,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5809\/revisions\/5811"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}