🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏
IntuitionThe goal is to construct a string using the characters of s such that:
No character appears consecutively more than repeatLimit times.
The resulting st
![image.png](https://assets.leetcode.com/users/images/e2ff2baf-a356-4316-9a4c-c0c7f88e0e3b_1734453460.0097964.png)\n\n\n---\n\n\n\n\n# Intuition\nThe goal is to construct a string using the characters of `s` such that:\n1. No character appears consecutively more than `repeatLimit` times.\n2. The resulting string is lexicographically largest.\n\nTo achieve this, we can:\n- Count the frequency of each character.\n- Start from the largest character (\'z\') and add it to the result as many times as allowed (up to `repeatLimit`).\n- If we hit the repeat limit but still have more of the current character left, we temporarily insert the next smaller character to "break" the repetition.\n\n---\n\n# Approach\n1. Use a frequency array to count occurrences of each character.\n2. Start from the largest character (\'z\') and try adding it up to `repeatLimit` times.\n3. If the repeat limit for the current character is reached:\n   - Find the next largest character with a positive frequency.\n   - Add it to the result to "break" the streak of repetitions.\n4. Continue this process until no characters remain.\n\n---\n\n# Complexity\n- **Time Complexity**:  \n  The time complexity is \\(O(n)\\), where \\(n\\) is the length of the string `s`.  \n  We process the string once to calculate frequencies and repeatedly check/insert characters based on their frequencies.\n\n- **Space Complexity**:  \n  \\(O(1)\\) for the frequency array since it always holds 26 entries (constant for lowercase letters).\n\n---\n\n\n```dart []\nimport \'dart:math\';\n\nclass Solution {\n  String repeatLimitedString(String s, int repeatLimit) {\n    List<int> freq = List.filled(26, 0);\n    int a = \'a\'.codeUnitAt(0);\n\n    // Step 1: Frequency of each character\n    for (var char in s.split(\'\')) {\n      freq[char.codeUnitAt(0) - a]++;\n    }\n\n    StringBuffer result = StringBuffer();\n    int index = 25;\n\n    // Step 2: Construct result string\n    while (index >= 0) {\n      if (freq[index] == 0) {\n        index--;\n        continue;\n      }\n\n      int repeatCount = min(freq[index], repeatLimit);\n      for (int i = 0; i < repeatCount; i++) {\n        result.write(String.fromCharCode(index + a));\n      }\n\n      freq[index] -= repeatCount;\n\n      if (freq[index] > 0) {\n        int nextIndex = index - 1;\n        while (nextIndex >= 0 && freq[nextIndex] == 0) {\n          nextIndex--;\n        }\n\n        if (nextIndex < 0) break;\n\n        result.write(String.fromCharCode(nextIndex + a));\n        freq[nextIndex]--;\n      } else {\n        index--;\n      }\n    }\n\n    return result.toString();\n  }\n}\n```\n\n\n```python []\nfrom collections import Counter\n\nclass Solution:\n    def repeatLimitedString(self, s: str, repeatLimit: int) -> str:\n        freq = [0] * 26\n        a_ord = ord(\'a\')\n\n        # Step 1: Frequency array\n        for char in s:\n            freq[ord(char) - a_ord] += 1\n\n        res = []\n        index = 25\n\n        # Step 2: Build result\n        while index >= 0:\n            if freq[index] == 0:\n                index -= 1\n                continue\n\n            repeat_count = min(freq[index], repeatLimit)\n            res.extend(chr(index + a_ord) * repeat_count)\n            freq[index] -= repeat_count\n\n            if freq[index] > 0:\n                next_index = index - 1\n                while next_index >= 0 and freq[next_index] == 0:\n                    next_index -= 1\n\n                if next_index < 0:\n                    break\n\n                res.append(chr(next_index + a_ord))\n                freq[next_index] -= 1\n            else:\n                index -= 1\n\n        return \'\'.join(res)\n```\n\n\n```cpp []\n#include <vector>\n#include <string>\n#include <algorithm>\nusing namespace std;\n\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int repeatLimit) {\n        vector<int> freq(26, 0);\n\n        // Step 1: Frequency array\n        for (char c : s) {\n            freq[c - \'a\']++;\n        }\n\n        string result;\n        int index = 25;\n\n        // Step 2: Build the result string\n        while (index >= 0) {\n            if (freq[index] == 0) {\n                index--;\n                continue;\n            }\n\n            int repeatCount = min(freq[index], repeatLimit);\n            result.append(repeatCount, \'a\' + index);\n            freq[index] -= repeatCount;\n\n            if (freq[index] > 0) {\n                int nextIndex = index - 1;\n                while (nextIndex >= 0 && freq[nextIndex] == 0) {\n                    nextIndex--;\n                }\n\n                if (nextIndex < 0) break;\n\n                result += \'a\' + nextIndex;\n                freq[nextIndex]--;\n            } else {\n                index--;\n            }\n        }\n\n        return result;\n    }\n};\n```\n\n\n```java []\nclass Solution {\n    public String repeatLimitedString(String s, int repeatLimit) {\n        int[] freq = new int[26];\n        for (char c : s.toCharArray()) {\n            freq[c - \'a\']++;\n        }\n\n        StringBuilder result = new StringBuilder();\n        int index = 25;\n\n        while (index >= 0) {\n            if (freq[index] == 0) {\n                index--;\n                continue;\n            }\n\n            int repeatCount = Math.min(freq[index], repeatLimit);\n            for (int i = 0; i < repeatCount; i++) {\n                result.append((char) (index + \'a\'));\n            }\n            freq[index] -= repeatCount;\n\n            if (freq[index] > 0) {\n                int nextIndex = index - 1;\n                while (nextIndex >= 0 && freq[nextIndex] == 0) {\n                    nextIndex--;\n                }\n\n                if (nextIndex < 0) break;\n\n                result.append((char) (nextIndex + \'a\'));\n                freq[nextIndex]--;\n            } else {\n                index--;\n            }\n        }\n\n        return result.toString();\n    }\n}\n```\n\n\n```javascript []\nclass Solution {\n  repeatLimitedString(s, repeatLimit) {\n    const freq = Array(26).fill(0);\n    const aCode = \'a\'.charCodeAt(0);\n\n    for (const char of s) {\n      freq[char.charCodeAt(0) - aCode]++;\n    }\n\n    let result = \'\';\n    let index = 25;\n\n    while (index >= 0) {\n      if (freq[index] === 0) {\n        index--;\n        continue;\n      }\n\n      const repeatCount = Math.min(freq[index], repeatLimit);\n      result += String.fromCharCode(index + aCode).repeat(repeatCount);\n      freq[index] -= repeatCount;\n\n      if (freq[index] > 0) {\n        let nextIndex = index - 1;\n        while (nextIndex >= 0 && freq[nextIndex] === 0) {\n          nextIndex--;\n        }\n\n        if (nextIndex < 0) break;\n\n        result += String.fromCharCode(nextIndex + aCode);\n        freq[nextIndex]--;\n      } else {\n        index--;\n      }\n    }\n\n    return result;\n  }\n}\n```\n\n---\n\n![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style=\'width:250px\'}
['String', 'Greedy', 'C', 'Counting', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'Dart']