{"id":103,"date":"2017-07-27T19:13:13","date_gmt":"2017-07-27T11:13:13","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=103"},"modified":"2018-11-27T16:09:32","modified_gmt":"2018-11-27T08:09:32","slug":"20-valid-parentheses","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=103","title":{"rendered":"20. Valid Parentheses"},"content":{"rendered":"<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><strong>Input:<\/strong> \"()\"\r\n<strong>Output:<\/strong> true\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre><strong>Input:<\/strong> \"()[]{}\"\r\n<strong>Output:<\/strong> true\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre><strong>Input:<\/strong> \"(]\"\r\n<strong>Output:<\/strong> false\r\n<\/pre>\n<p><strong>Example 4:<\/strong><\/p>\n<pre><strong>Input:<\/strong> \"([)]\"\r\n<strong>Output:<\/strong> false\r\n<\/pre>\n<p><strong>Example 5:<\/strong><\/p>\n<pre><strong>Input:<\/strong> \"{[]}\"\r\n<strong>Output:<\/strong> true<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def isValid(self, s):\r\n        &quot;&quot;&quot;\r\n        '(', ')', '{', '}', '[' and ']'\r\n        :type s: str\r\n        :rtype: bool\r\n        &quot;&quot;&quot;\r\n        temp_list = []\r\n        for i in s:\r\n            if i == &quot;(&quot; or i == &quot;{&quot; or i == &quot;[&quot;:\r\n                temp_list.append(i)\r\n            else:\r\n                try:\r\n                    if i == &quot;)&quot; and temp_list[-1] != &quot;(&quot; or i == &quot;}&quot; and temp_list[-1] != &quot;{&quot; or i == &quot;]&quot; and temp_list[-1] != &quot;[&quot;:\r\n                        return False\r\n                    temp_list.pop()\r\n                except:\r\n                    return False\r\n        return not temp_list\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a string containing just the characters\u00a0&#8216;(&#8216;,\u00a0&#8216;)&#8217;,<a href=\"https:\/\/www.ccagml.com\/?p=103\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">20. Valid Parentheses<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/103"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=103"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/103\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}