{"id":4183,"date":"2018-10-13T20:03:59","date_gmt":"2018-10-14T03:03:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4183"},"modified":"2018-10-13T20:04:27","modified_gmt":"2018-10-14T03:04:27","slug":"leetcode-921-minimum-add-to-make-parentheses-valid","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-921-minimum-add-to-make-parentheses-valid\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 921. Minimum Add to Make Parentheses Valid"},"content":{"rendered":"<p>Given a string\u00a0<code>S<\/code>\u00a0of\u00a0<code>'('<\/code>\u00a0and\u00a0<code>')'<\/code>\u00a0parentheses, we add the minimum number of parentheses (\u00a0<code>'('<\/code>\u00a0or\u00a0<code>')'<\/code>, and in any positions ) so that the resulting parentheses string is valid.<\/p>\n<p>Formally, a parentheses string is valid if and only if:<\/p>\n<ul>\n<li>It is the empty string, or<\/li>\n<li>It can be written as\u00a0<code>AB<\/code>\u00a0(<code>A<\/code>\u00a0concatenated with\u00a0<code>B<\/code>), where\u00a0<code>A<\/code>\u00a0and\u00a0<code>B<\/code>\u00a0are valid strings, or<\/li>\n<li>It can be written as\u00a0<code>(A)<\/code>, where\u00a0<code>A<\/code>\u00a0is a valid string.<\/li>\n<\/ul>\n<p>Given a parentheses string, return the minimum number of parentheses we must add to make the resulting string valid.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">\"())\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">1<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">\"(((\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">3<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">\"()\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">0<\/span>\r\n<\/pre>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-4-1\">\"()))((\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-4\">4<\/span><\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>S.length &lt;= 1000<\/code><\/li>\n<li><code>S<\/code>\u00a0only consists of\u00a0<code>'('<\/code>\u00a0and\u00a0<code>')'<\/code>\u00a0characters.<\/li>\n<\/ol>\n<h1><strong>Solution: Counting<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\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\nclass Solution {\r\npublic:\r\n  int minAddToMakeValid(string S) {\r\n    int l = 0;    \r\n    int m = 0;\r\n    for (char c : S) {\r\n      if (c == '(') ++l;\r\n      if (c == ')' &amp;&amp; l &gt; 0) {\r\n        --l;\r\n        ++m;\r\n      }\r\n    }\r\n    return S.size() - m * 2;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string\u00a0S\u00a0of\u00a0&#8216;(&#8216;\u00a0and\u00a0&#8216;)&#8217;\u00a0parentheses, we add the minimum number of parentheses (\u00a0&#8216;(&#8216;\u00a0or\u00a0&#8216;)&#8217;, and in any positions ) so that the resulting parentheses string is valid. Formally,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[177,421,4],"class_list":["post-4183","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-medium","tag-parentheses","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4183","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=4183"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4183\/revisions"}],"predecessor-version":[{"id":4186,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4183\/revisions\/4186"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}