🌟 Greedy Approach: Simple & Beginner-Friendly🚀 | 💥 Beats 95% 💪
Intuition 🤔:We aim to create the lexicographically largest string. Using a greedy approach:
Pick the largest character 🎯 (e.g., 'z') as much as allowed by repea
# Intuition \uD83E\uDD14:\nWe aim to create the lexicographically largest string. Using a greedy approach:\n\n1. Pick the largest character \uD83C\uDFAF (e.g., \'z\') as much as allowed by repeatLimit \u23F3.\n2. Once we hit the limit, move to the next largest character \uD83D\uDD3D.\n3. Repeat this until we\u2019ve used all characters from the string \uD83D\uDCDD.\n\n---\n\n# Approach \uD83D\uDEE0\uFE0F:\n1. Count frequencies of each character \uD83D\uDCCA.\n2. Greedily add characters from largest to smallest \uD83C\uDF1F.\n- Add up to repeatLimit times from the largest available letter \uD83D\uDD25.\n3. Stop when all letters are used \u23F9\uFE0F.\n\n---\n\n# Complexity\n- Time Complexity \u23F1\uFE0F:\n*$$O(n)$$*, where n is the length of the string \u2728.\n\n\n- Space Complexity \uD83D\uDCBE:\n*$$O(1)$$*, constant space used for counting 26 letters \uD83C\uDFAF.\n\n---\n\n## Why This Works \uD83D\uDCAA:\n- Greedy = Best \uD83D\uDCA1! Always choose the largest possible letter at each step for the largest lexicographical result \uD83C\uDF89.\n- The algorithm is efficient and fast\u2014Beats 95%! \uD83C\uDFC5\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int repeatLimit) {\n        vector<int> letterCount (26, 0);\n        for(int i = 0; i < s.size(); i++) {\n            letterCount[s[i]-\'a\']++;\n        }\n\n        string ans = "";\n        int index = 25;\n        while(true) {\n            int countToAdd = min(repeatLimit, letterCount[index]);\n            if(countToAdd > 0) {\n                ans.append(countToAdd, \'a\' + index);\n                letterCount[index] -= countToAdd;\n            }\n\n            int nextIndex = index-1;\n            if(letterCount[index] > 0) {\n                while(nextIndex >= 0 && letterCount[nextIndex] == 0) {\n                    nextIndex--;\n                }\n                if(nextIndex < 0) break;\n\n                ans += (\'a\' + nextIndex);\n                letterCount[nextIndex]--;\n            } else {\n                while(index >= 0 && letterCount[index] == 0) {\n                    index--;\n                }\n                if(index < 0) break;\n            }\n        }\n        return ans;\n    }\n};\n```\n\n```java []\nimport java.util.*;\n\nclass Solution {\n    public String repeatLimitedString(String s, int repeatLimit) {\n        int[] letterCount = new int[26];\n        for (char c : s.toCharArray()) {\n            letterCount[c - \'a\']++;\n        }\n\n        StringBuilder ans = new StringBuilder();\n        int index = 25;\n\n        while (true) {\n            int countToAdd = Math.min(repeatLimit, letterCount[index]);\n            if (countToAdd > 0) {\n                for (int i = 0; i < countToAdd; i++) {\n                    ans.append((char) (\'a\' + index));\n                }\n                letterCount[index] -= countToAdd;\n            }\n\n            int nextIndex = index - 1;\n            if (letterCount[index] > 0) {\n                while (nextIndex >= 0 && letterCount[nextIndex] == 0) {\n                    nextIndex--;\n                }\n                if (nextIndex < 0) break;\n\n                ans.append((char) (\'a\' + nextIndex));\n                letterCount[nextIndex]--;\n            } else {\n                while (index >= 0 && letterCount[index] == 0) {\n                    index--;\n                }\n                if (index < 0) break;\n            }\n        }\n\n        return ans.toString();\n    }\n}\n\n```\n```javascript []\nvar repeatLimitedString = function(s, repeatLimit) {\n    let letterCount = new Array(26).fill(0);\n    for (let char of s) {\n        letterCount[char.charCodeAt(0) - \'a\'.charCodeAt(0)]++;\n    }\n\n    let ans = "";\n    let index = 25;\n\n    while (true) {\n        let countToAdd = Math.min(repeatLimit, letterCount[index]);\n        if (countToAdd > 0) {\n            ans += String.fromCharCode(\'a\'.charCodeAt(0) + index).repeat(countToAdd);\n            letterCount[index] -= countToAdd;\n        }\n\n        let nextIndex = index - 1;\n        if (letterCount[index] > 0) {\n            while (nextIndex >= 0 && letterCount[nextIndex] === 0) {\n                nextIndex--;\n            }\n            if (nextIndex < 0) break;\n\n            ans += String.fromCharCode(\'a\'.charCodeAt(0) + nextIndex);\n            letterCount[nextIndex]--;\n        } else {\n            while (index >= 0 && letterCount[index] === 0) {\n                index--;\n            }\n            if (index < 0) break;\n        }\n    }\n\n    return ans;\n};\n\n```\n```python []\nclass Solution:\n    def repeatLimitedString(self, s: str, repeatLimit: int) -> str:\n        letterCount = [0] * 26\n        for c in s:\n            letterCount[ord(c) - ord(\'a\')] += 1\n\n        ans = []\n        index = 25\n\n        while True:\n            countToAdd = min(repeatLimit, letterCount[index])\n            if countToAdd > 0:\n                ans.extend(chr(ord(\'a\') + index) * countToAdd)\n                letterCount[index] -= countToAdd\n\n            nextIndex = index - 1\n            if letterCount[index] > 0:\n                while nextIndex >= 0 and letterCount[nextIndex] == 0:\n                    nextIndex -= 1\n                if nextIndex < 0: break\n\n                ans.append(chr(ord(\'a\') + nextIndex))\n                letterCount[nextIndex] -= 1\n            else:\n                while index >= 0 and letterCount[index] == 0:\n                    index -= 1\n                if index < 0: break\n\n        return "".join(ans)\n\n```\n```ruby []\ndef repeat_limited_string(s, repeat_limit)\n  letter_count = Array.new(26, 0)\n  s.each_char { |c| letter_count[c.ord - \'a\'.ord] += 1 }\n\n  ans = ""\n  index = 25\n\n  while true\n    count_to_add = [repeat_limit, letter_count[index]].min\n    if count_to_add > 0\n      ans << ((\'a\'.ord + index).chr * count_to_add)\n      letter_count[index] -= count_to_add\n    end\n\n    next_index = index - 1\n    if letter_count[index] > 0\n      next_index -= 1 while next_index >= 0 && letter_count[next_index] == 0\n      break if next_index < 0\n\n      ans << ((\'a\'.ord + next_index).chr)\n      letter_count[next_index] -= 1\n    else\n      index -= 1 while index >= 0 && letter_count[index] == 0\n      break if index < 0\n    end\n  end\n\n  ans\nend\n\n```\n```rust []\nimpl Solution {\n    pub fn repeat_limited_string(s: String, repeat_limit: i32) -> String {\n        let mut letter_count = vec![0; 26];\n        for c in s.chars() {\n            letter_count[(c as usize) - (\'a\' as usize)] += 1;\n        }\n\n        let mut ans = String::new();\n        let mut index = 25;\n\n        loop {\n            let count_to_add = repeat_limit.min(letter_count[index]);\n            if count_to_add > 0 {\n                ans.push_str(&std::iter::repeat((b\'a\' + index as u8) as char)\n                    .take(count_to_add as usize)\n                    .collect::<String>());\n                letter_count[index] -= count_to_add;\n            }\n\n            let mut next_index = index - 1;\n            if letter_count[index] > 0 {\n                while next_index > 0 && letter_count[next_index] == 0 {\n                    next_index -= 1;\n                }\n                if next_index == 0 && letter_count[next_index] == 0 {\n                    break;\n                }\n\n                ans.push((b\'a\' + next_index as u8) as char);\n                letter_count[next_index] -= 1;\n            } else {\n                while index > 0 && letter_count[index] == 0 {\n                    index -= 1;\n                }\n                if index == 0 && letter_count[index] == 0 {\n                    break;\n                }\n            }\n        }\n\n        ans\n    }\n}\n\n```\n```c# []\nusing System;\nusing System.Text;\n\npublic class Solution {\n    public string RepeatLimitedString(string s, int repeatLimit) {\n        int[] letterCount = new int[26];\n        foreach (char c in s) {\n            letterCount[c - \'a\']++;\n        }\n\n        StringBuilder ans = new StringBuilder();\n        int index = 25;\n\n        while (true) {\n            int countToAdd = Math.Min(repeatLimit, letterCount[index]);\n            if (countToAdd > 0) {\n                ans.Append(new string((char)(\'a\' + index), countToAdd));\n                letterCount[index] -= countToAdd;\n            }\n\n            int nextIndex = index - 1;\n            if (letterCount[index] > 0) {\n                while (nextIndex >= 0 && letterCount[nextIndex] == 0) {\n                    nextIndex--;\n                }\n                if (nextIndex < 0) break;\n\n                ans.Append((char)(\'a\' + nextIndex));\n                letterCount[nextIndex]--;\n            } else {\n                while (index >= 0 && letterCount[index] == 0) {\n                    index--;\n                }\n                if (index < 0) break;\n            }\n        }\n\n        return ans.ToString();\n    }\n}\n\n```\n```go []\nfunc repeatLimitedString(s string, repeatLimit int) string {\n    letterCount := make([]int, 26)\n    for _, c := range s {\n        letterCount[c-\'a\']++\n    }\n\n    ans := ""\n    index := 25\n\n    for {\n        countToAdd := min(repeatLimit, letterCount[index])\n        if countToAdd > 0 {\n            ans += strings.Repeat(string(\'a\'+index), countToAdd)\n            letterCount[index] -= countToAdd\n        }\n\n        nextIndex := index - 1\n        if letterCount[index] > 0 {\n            for nextIndex >= 0 && letterCount[nextIndex] == 0 {\n                nextIndex--\n            }\n            if nextIndex < 0 {\n                break\n            }\n\n            ans += string(\'a\' + nextIndex)\n            letterCount[nextIndex]--\n        } else {\n            for index >= 0 && letterCount[index] == 0 {\n                index--\n            }\n            if index < 0 {\n                break\n            }\n        }\n    }\n    return ans\n}\n\nfunc min(a, b int) int {\n    if a < b {\n        return a\n    }\n    return b\n}\n\n```\n
['Hash Table', 'Greedy', 'Python', 'C++', 'Java', 'Go', 'Rust', 'Ruby', 'JavaScript', 'C#']