{"id":5550,"date":"2019-09-15T15:16:24","date_gmt":"2019-09-15T22:16:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5550"},"modified":"2019-09-15T15:21:47","modified_gmt":"2019-09-15T22:21:47","slug":"leetcode-1190-reverse-substrings-between-each-pair-of-parentheses","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-1190-reverse-substrings-between-each-pair-of-parentheses\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1190. Reverse Substrings Between Each Pair of Parentheses"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;that consists of lower case English letters and brackets.&nbsp;<\/p>\n\n\n\n<p>Reverse the strings&nbsp;in each&nbsp;pair of matching parentheses, starting&nbsp;from the innermost one.<\/p>\n\n\n\n<p>Your result should&nbsp;<strong>not<\/strong>&nbsp;contain any bracket.<\/p>\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 = \"(abcd)\"\n<strong>Output:<\/strong> \"dcba\"\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 = \"(u(love)i)\"\n<strong>Output:<\/strong> \"iloveu\"\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 = \"(ed(et(oc))el)\"\n<strong>Output:<\/strong> \"leetcode\"\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(bcdefghijkl(mno)p)q\"\n<strong>Output:<\/strong> \"apmnolkjihgfedcbq\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= s.length &lt;= 2000<\/code><\/li><li><code>s<\/code>&nbsp;only contains lower case English characters and parentheses.<\/li><li>It&#8217;s guaranteed that all parentheses are balanced.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Stack<\/strong><\/h2>\n\n\n\n<p>Use a stack of strings to track all the active strings.<br>Iterate over the input string:<br>1. Whenever there is a &#8216;(&#8216;, push an empty string to the stack.<br>2. Whenever this is a &#8216;)&#8217;, pop the top string and append the reverse of it to the new stack top.<br>3. Otherwise, append the letter to the string on top the of stack.<br><br>Once done, the (only) string on the top of the stack is the answer.<\/p>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string reverseParentheses(string s) {\n    stack<string> st;\n    st.push(\"\");\n    for (char c : s) {\n      if (c == '(') st.push(\"\");\n      else if (c != ')') st.top() += c;\n      else {\n        string t = st.top(); st.pop();\n        st.top().insert(end(st.top()), rbegin(t), rend(t));\n      }\n    }\n    return st.top();\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;that consists of lower case English letters and brackets.&nbsp; Reverse the strings&nbsp;in each&nbsp;pair of matching parentheses, starting&nbsp;from the innermost one. Your result should&nbsp;not&nbsp;contain&#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":[177,498,246,180,4],"class_list":["post-5550","post","type-post","status-publish","format-standard","hentry","category-stack","tag-medium","tag-on2","tag-reverse","tag-stack","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5550","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=5550"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5550\/revisions"}],"predecessor-version":[{"id":5552,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5550\/revisions\/5552"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}