{"id":9327,"date":"2021-12-31T13:34:55","date_gmt":"2021-12-31T21:34:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9327"},"modified":"2021-12-31T13:35:54","modified_gmt":"2021-12-31T21:35:54","slug":"leetcode-1957-delete-characters-to-make-fancy-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1957-delete-characters-to-make-fancy-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1957. Delete Characters to Make Fancy String"},"content":{"rendered":"\n<p>A&nbsp;<strong>fancy string<\/strong>&nbsp;is a string where no&nbsp;<strong>three<\/strong>&nbsp;<strong>consecutive<\/strong>&nbsp;characters are equal.<\/p>\n\n\n\n<p>Given a string&nbsp;<code>s<\/code>, delete the&nbsp;<strong>minimum<\/strong>&nbsp;possible number of characters from&nbsp;<code>s<\/code>&nbsp;to make it&nbsp;<strong>fancy<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the final string after the deletion<\/em>. It can be shown that the answer will always be&nbsp;<strong>unique<\/strong>.<\/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 = \"leeetcode\"\n<strong>Output:<\/strong> \"leetcode\"\n<strong>Explanation:<\/strong>\nRemove an 'e' from the first group of 'e's to create \"leetcode\".\nNo three consecutive characters are equal, so return \"leetcode\".\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 = \"aaabaaaa\"\n<strong>Output:<\/strong> \"aabaa\"\n<strong>Explanation:<\/strong>\nRemove an 'a' from the first group of 'a's to create \"aabaaaa\".\nRemove two 'a's from the second group of 'a's to create \"aabaa\".\nNo three consecutive characters are equal, so return \"aabaa\".\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 = \"aab\"\n<strong>Output:<\/strong> \"aab\"\n<strong>Explanation:<\/strong> No three consecutive characters are equal, so return \"aab\".\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<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;consists only of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:<\/strong><\/h2>\n\n\n\n<p>Skip the current letter if there are already two same letters in the output string.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  string makeFancyString(string s) {\n    string ans;\n    int count = 0;\n    for (char c : s) {\n      if (ans.empty() || c != ans.back()) \n        count = 1;\n      else if (++count >= 3) continue;\n      ans += c;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;fancy string&nbsp;is a string where no&nbsp;three&nbsp;consecutive&nbsp;characters are equal. Given a string&nbsp;s, delete the&nbsp;minimum&nbsp;possible number of characters from&nbsp;s&nbsp;to make it&nbsp;fancy. Return&nbsp;the final string after the deletion.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,4],"class_list":["post-9327","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9327","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=9327"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9327\/revisions"}],"predecessor-version":[{"id":9331,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9327\/revisions\/9331"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}