Very Easy Detailed Solution 🔥 || 🎯 Greedy Algorithm
IntuitionWe will make a frequency array to track the frequency of each character in the string, and will iterate from last in order the maintain the lexicograph
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe will make a frequency array to track the frequency of each character in the string, and will iterate from last in order the maintain the lexicographical order. We will append the char untill reached the limit of its frequency gets 0. If we sort by limit we will check for closest smaller char and will add one of that, acting as a separator.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Frequency Calculation:**\n    - The frequency of each character in the string s is calculated and stored in an array freq[] of size 26.\n    - freq[i] represents the count of character (char)(\'a\' + i).\n\n2. **Greedy Approach with Lexicographical Order:**\n    - The algorithm processes characters in descending lexicographical order (from \'z\' to \'a\').\n    - For each character:\n        - Append the character to the result string at most repeatLimit times (using min(freq[i], repeatLimit)).\n        - Decrement its frequency after appending.\n    - If a character still has remaining frequency but cannot be added due to the repeatLimit, insert a smaller lexicographical character that still has frequency left:\n        - Find the next valid character (j < i) with a non-zero frequency.\n        - Append this smaller character to the result.\n        - Decrement its frequency.\n        - To ensure the larger character (i) can resume appending later, increment i (effectively reprocessing it).\n\n3. **Building the Result:**\n    - Use a StringBuilder to efficiently append characters and build the result string.\n\n4. **Termination:**\n    - The loop continues until all characters have been processed and their frequencies reach zero.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$O(n)$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$O(n)$\n# Code\n```java []\nclass Solution {\n    public String repeatLimitedString(String s, int repeatLimit) {\n        int n = s.length();\n        StringBuilder sb = new StringBuilder();\n\n        int freq[] = new int[26];\n        for(char ch : s.toCharArray()){\n            freq[ch - \'a\']++;\n        }\n\n        for(int i=25; i>=0; i--){\n            if(freq[i] == 0) continue;\n\n            int minAppend = Math.min(freq[i], repeatLimit);\n            freq[i] -= minAppend;\n            while(minAppend > 0){\n                sb.append((char)(\'a\' + i));\n                minAppend--;\n            }\n\n            if(freq[i] != 0){\n                for(int j=i-1; j>=0; j--){\n                    if(freq[j] == 0) continue;\n                    sb.append((char)(\'a\' + j));\n                    freq[j]--;\n                    i++;\n                    break;\n                }\n            }\n        }\n        return sb.toString();\n    }\n}\n```
['Array', 'String', 'Greedy', 'Counting', 'Java']