{"id":4110,"date":"2018-10-02T00:40:04","date_gmt":"2018-10-02T07:40:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4110"},"modified":"2018-10-02T00:44:12","modified_gmt":"2018-10-02T07:44:12","slug":"leetcode-20-valid-parentheses","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-20-valid-parentheses\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 20. Valid Parentheses"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a string containing just the characters\u00a0<code>'('<\/code>,\u00a0<code>')'<\/code>,\u00a0<code>'{'<\/code>,\u00a0<code>'}'<\/code>,\u00a0<code>'['<\/code>\u00a0and\u00a0<code>']'<\/code>, determine if the input string is valid.<\/p>\n<p>An input string is valid if:<\/p>\n<ol>\n<li>Open brackets must be closed by the same type of brackets.<\/li>\n<li>Open brackets must be closed in the correct order.<\/li>\n<\/ol>\n<p>Note that an empty string is\u00a0also considered valid.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"()\"\r\n<strong>Output:<\/strong> true\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"()[]{}\"\r\n<strong>Output:<\/strong> true\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"(]\"\r\n<strong>Output:<\/strong> false\r\n<\/pre>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"([)]\"\r\n<strong>Output:<\/strong> false\r\n<\/pre>\n<p><strong>Example 5:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> \"{[]}\"\r\n<strong>Output:<\/strong> true\r\n<\/pre>\n<h1><strong>Solution: Stack<\/strong><\/h1>\n<p>Using a stack to track the existing open parentheses, if the current one is a close\u00a0parenthesis but does not match the top of the stack, return false, otherwise pop the stack. Check whether the stack is empty in the end.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/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, 0 ms\r\nclass Solution {\r\npublic:\r\n  bool isValid(string s) {\r\n    map&lt;char, char&gt; p{{')', '('}, {']', '['}, {'}', '{'}};\r\n    stack&lt;char&gt; st;    \r\n    for (char c : s) {\r\n      if (!p.count(c)) {\r\n        st.push(c);\r\n      } else {\r\n        if (st.empty() || p[c] != st.top()) return false;        \r\n        st.pop();\r\n      }\r\n    }\r\n    return st.empty();\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \"># Author: Huahua\r\nclass Solution:\r\n  def isValid(self, s):\r\n    p = {')' : '(', ']': '[', '}' : '{'}\r\n    stack = []\r\n    for c in s:\r\n      if c not in p:\r\n        stack.append(c)\r\n      else:\r\n        if not stack or stack[-1] != p[c]: return False\r\n        stack.pop()\r\n    return not stack<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-22-generate-parentheses\/\">\u82b1\u82b1\u9171 LeetCode 22. Generate Parentheses<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-301-remove-invalid-parentheses\/\">\u82b1\u82b1\u9171 LeetCode 301. Remove Invalid Parentheses<\/a><\/li>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-856-score-of-parentheses\/\">\u82b1\u82b1\u9171 LeetCode 856. Score of Parentheses<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a string containing just the characters\u00a0&#8216;(&#8216;,\u00a0&#8216;)&#8217;,\u00a0&#8216;{&#8216;,\u00a0&#8216;}&#8217;,\u00a0&#8216;[&#8216;\u00a0and\u00a0&#8216;]&#8217;, determine if the input string is valid. An input string is valid if: Open brackets must be&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[222,421,180],"class_list":["post-4110","post","type-post","status-publish","format-standard","hentry","category-stack","tag-easy","tag-parentheses","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4110","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=4110"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4110\/revisions"}],"predecessor-version":[{"id":4115,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4110\/revisions\/4115"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}