{"id":1304,"date":"2017-12-21T16:23:07","date_gmt":"2017-12-22T00:23:07","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1304"},"modified":"2018-08-30T14:04:53","modified_gmt":"2018-08-30T21:04:53","slug":"leetcode-301-remove-invalid-parentheses","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-301-remove-invalid-parentheses\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 301. Remove Invalid Parentheses"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 301. Remove Invalid Parentheses - \u5237\u9898\u627e\u5de5\u4f5c EP139\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/2k_rS_u6EBk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.<\/p>\n<p>Note: The input string may contain letters other than the parentheses\u00a0<code>(<\/code>\u00a0and\u00a0<code>)<\/code>.<\/p>\n<p><b>Examples:<\/b><\/p>\n<pre class=\"show-plain-default:true lang:default decode:true \">\"()())()\" -&gt; [\"()()()\", \"(())()\"]\r\n\"(a)())()\" -&gt; [\"(a)()()\", \"(a())()\"]\r\n\")(\" -&gt; [\"\"]\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7531&#8221;(&#8221; &#8220;)&#8221;\u548c\u5176\u4ed6\u5b57\u7b26\u6784\u6210\u3002\u8ba9\u4f60\u5220\u9664\u6570\u91cf\u6700\u5c11\u7684\u62ec\u53f7\u4f7f\u5f97\u8868\u8fbe\u5f0f\u5408\u6cd5\uff08\u62ec\u53f7\u90fd\u5339\u914d\uff09\u3002\u8f93\u51fa\u6240\u6709\u7684\u5408\u6cd5\u8868\u8fbe\u5f0f\u3002<\/p>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\">\u00a0<\/ins><\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>Search\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1317\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/301-ep139-1-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/301-ep139-1-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/301-ep139-1-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/301-ep139-1-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1314\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/301-ep139-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/301-ep139-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/301-ep139-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/301-ep139-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1><strong>Solution: DFS<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 0 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;string&gt; removeInvalidParentheses(const string&amp; s) {        \r\n    int l = 0;\r\n    int r = 0;\r\n\r\n    for (const char ch : s) {\r\n      l += (ch == '(');\r\n      if (l == 0)\r\n        r += (ch == ')');\r\n      else\r\n        l -= (ch == ')');\r\n    }\r\n\r\n    vector&lt;string&gt; ans;\r\n    dfs(s, 0, l, r, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  bool isValid(const string&amp; s) {\r\n    int count = 0;\r\n    for (const char ch : s) {\r\n      if (ch == '(') ++count;\r\n      if (ch == ')') --count;\r\n      if (count &lt; 0) return false;\r\n    }\r\n    return count == 0;\r\n  }\r\n\r\n  \/\/ l\/r: number of left\/right parentheses to remove.\r\n  void dfs(const string&amp; s, int start, int l, int r, vector&lt;string&gt;&amp; ans) {\r\n    \/\/ Nothing to remove.\r\n    if (l == 0 &amp;&amp; r == 0) {\r\n      if (isValid(s)) ans.push_back(s);\r\n      return;\r\n    }\r\n\r\n    for (int i = start; i &lt; s.length(); ++i) {\r\n      \/\/ We only remove the first parenthes if there are consecutive ones to avoid duplications.\r\n      if (i != start &amp;&amp; s[i] == s[i - 1]) continue;\r\n\r\n      if (s[i] == '(' || s[i] == ')') {\r\n        string curr = s;\r\n        curr.erase(i, 1);\r\n        if (r &gt; 0 &amp;&amp; s[i] == ')') \r\n          dfs(curr, i, l, r - 1, ans);\r\n        else if (l &gt; 0 &amp;&amp; s[i] == '(')\r\n          dfs(curr, i, l - 1, r, ans);\r\n      }\r\n    }\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[165,44,47],"tags":[33,217],"class_list":["post-1304","post","type-post","status-publish","format-standard","hentry","category-hard","category-searching","category-string","tag-dfs","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1304","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=1304"}],"version-history":[{"count":12,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1304\/revisions"}],"predecessor-version":[{"id":3780,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1304\/revisions\/3780"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}