{"id":5597,"date":"2019-09-29T00:47:24","date_gmt":"2019-09-29T07:47:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5597"},"modified":"2019-09-29T00:48:38","modified_gmt":"2019-09-29T07:48:38","slug":"leetcode-1209-remove-all-adjacent-duplicates-in-string-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-1209-remove-all-adjacent-duplicates-in-string-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1209. Remove All Adjacent Duplicates in String II"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>, a&nbsp;<em>k<\/em>&nbsp;<em>duplicate removal<\/em>&nbsp;consists of choosing&nbsp;<code>k<\/code>&nbsp;adjacent and equal letters from&nbsp;<code>s<\/code>&nbsp;and removing&nbsp;them causing the left and the right side of the deleted substring to concatenate together.<\/p>\n\n\n\n<p>We repeatedly make&nbsp;<code>k<\/code>&nbsp;duplicate removals on&nbsp;<code>s<\/code>&nbsp;until we no longer can.<\/p>\n\n\n\n<p>Return the final string after all such duplicate removals have been made.<\/p>\n\n\n\n<p>It is guaranteed that the answer is unique.<\/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\", k = 2\n<strong>Output:<\/strong> \"abcd\"\n<strong>Explanation: <\/strong>There's nothing to delete.<\/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 = \"deeedbbcccbdaa\", k = 3\n<strong>Output:<\/strong> \"aa\"\n<strong>Explanation: \n<\/strong>First delete \"eee\" and \"ccc\", get \"ddbbbdaa\"\nThen delete \"bbb\", get \"dddaa\"\nFinally delete \"ddd\", get \"aa\"<\/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 = \"pbbcggttciiippooaais\", k = 2\n<strong>Output:<\/strong> \"ps\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 10^5<\/code><\/li><li><code>2 &lt;= k &lt;= 10^4<\/code><\/li><li><code>s<\/code>&nbsp;only contains lower case English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Stack<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<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 removeDuplicates(string s, int k) {\n    vector<pair<char, int>> st{{'*', 0}};\n    for (char c : s)\n      if (c != st.back().first)\n        st.emplace_back(c, 1);\n      else if (++st.back().second == k)\n        st.pop_back();      \n    string ans;\n    for (const auto& p : st)\n      ans.append(p.second, p.first);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s, a&nbsp;k&nbsp;duplicate removal&nbsp;consists of choosing&nbsp;k&nbsp;adjacent and equal letters from&nbsp;s&nbsp;and removing&nbsp;them causing the left and the right side of the deleted substring to concatenate&#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,180],"class_list":["post-5597","post","type-post","status-publish","format-standard","hentry","category-stack","tag-medium","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5597","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=5597"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5597\/revisions"}],"predecessor-version":[{"id":5600,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5597\/revisions\/5600"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}