{"id":9526,"date":"2022-03-03T04:09:52","date_gmt":"2022-03-03T12:09:52","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9526"},"modified":"2022-03-03T04:10:19","modified_gmt":"2022-03-03T12:10:19","slug":"leetcode-2182-construct-string-with-repeat-limit","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2182-construct-string-with-repeat-limit\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2182. Construct String With Repeat Limit"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;and an integer&nbsp;<code>repeatLimit<\/code>. Construct a new string&nbsp;<code>repeatLimitedString<\/code>&nbsp;using the characters of&nbsp;<code>s<\/code>&nbsp;such that no letter appears&nbsp;<strong>more than<\/strong>&nbsp;<code>repeatLimit<\/code>&nbsp;times&nbsp;<strong>in a row<\/strong>. You do&nbsp;<strong>not<\/strong>&nbsp;have to use all characters from&nbsp;<code>s<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>lexicographically largest<\/strong>&nbsp;<\/em><code>repeatLimitedString<\/code>&nbsp;<em>possible<\/em>.<\/p>\n\n\n\n<p>A string&nbsp;<code>a<\/code>&nbsp;is&nbsp;<strong>lexicographically larger<\/strong>&nbsp;than a string&nbsp;<code>b<\/code>&nbsp;if in the first position where&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;differ, string&nbsp;<code>a<\/code>&nbsp;has a letter that appears later in the alphabet than the corresponding letter in&nbsp;<code>b<\/code>. If the first&nbsp;<code>min(a.length, b.length)<\/code>&nbsp;characters do not differ, then the longer string is the lexicographically larger one.<\/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 = \"cczazcc\", repeatLimit = 3\n<strong>Output:<\/strong> \"zzcccac\"\n<strong>Explanation:<\/strong> We use all of the characters from s to construct the repeatLimitedString \"zzcccac\".\nThe letter 'a' appears at most 1 time in a row.\nThe letter 'c' appears at most 3 times in a row.\nThe letter 'z' appears at most 2 times in a row.\nHence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.\nThe string is the lexicographically largest repeatLimitedString possible so we return \"zzcccac\".\nNote that the string \"zzcccca\" is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString.\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 = \"aababab\", repeatLimit = 2\n<strong>Output:<\/strong> \"bbabaa\"\n<strong>Explanation:<\/strong> We use only some of the characters from s to construct the repeatLimitedString \"bbabaa\". \nThe letter 'a' appears at most 2 times in a row.\nThe letter 'b' appears at most 2 times in a row.\nHence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.\nThe string is the lexicographically largest repeatLimitedString possible so we return \"bbabaa\".\nNote that the string \"bbabaaa\" is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= repeatLimit &lt;= s.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;consists of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Adding one letter at a time, find the largest one that can be used.<\/p>\n\n\n\n<p>Time complexity: O(26*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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string repeatLimitedString(string s, int repeatLimit) {\n    vector<int> m(26);\n    for (char c : s) ++m[c - 'a'];\n    string ans;\n    int count = 0;\n    int last = -1;\n    while (true) {\n      bool found = false;      \n      for (int i = 25; i >= 0 && !found; --i)\n        if (m[i] && (count < repeatLimit || last != i)) {\n          ans += 'a' + i;\n          count = (last == i) * count + 1;\n          --m[i];\n          last = i;\n          found = true;          \n        }\n      if (!found) break;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;and an integer&nbsp;repeatLimit. Construct a new string&nbsp;repeatLimitedString&nbsp;using the characters of&nbsp;s&nbsp;such that no letter appears&nbsp;more than&nbsp;repeatLimit&nbsp;times&nbsp;in a row. You do&nbsp;not&nbsp;have to use&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,82,177,4],"class_list":["post-9526","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-hashtable","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9526","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=9526"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9526\/revisions"}],"predecessor-version":[{"id":9529,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9526\/revisions\/9529"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}