Solution for LeetCode#2182
IntuitionThe problem requires creating a lexicographically largest string with a limit on consecutive repeating characters. The intuition is to use a greedy app
![image.png](https://assets.leetcode.com/users/images/bf7a2ebc-e1d8-4d3c-974a-41ae212f43aa_1734412686.5855422.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires creating a lexicographically largest string with a limit on consecutive repeating characters. The intuition is to use a greedy approach, starting with the largest character and working our way down.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Count the frequency of each character in the input string.\n2. Start from the largest character (z) and work downwards.\n3. For each character, add it to the result string up to the repeat limit.\n4. If there are more occurrences of a character than the repeat limit, find the next available smaller character to insert as a separator.\n5. Repeat this process until all characters are used.\n\n# Complexity\n- Time complexity: O(n+k)\n    - n: Total number of characters in the input string.\n    - k: Number of unique characters in the input string (at most 26 for lowercase letters).\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(26)\u2192O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {  \n\n    public String repeatLimitedString(String s, int repeatLimit) {\n        int n=s.length();\n        StringBuilder sb=new StringBuilder();\n\n        // Step 1: Frequency count for each character\n        int[] arr=new int[26];\n        for(int i=0; i<n; i++){\n            arr[s.charAt(i)-\'a\']++;\n        }\n\n        // Step 2: Iterate from larger characters to smaller characters\n        for(int i=25; i>=0; i--){ // i: One pointer\n            if(arr[i]==0) continue;\n            \n            int curr=Math.min(arr[i], repeatLimit);\n            arr[i]-=curr;\n            while(curr>0){ \n                sb.append( (char)(\'a\'+i) );\n                curr--;\n            }                        \n\n            // Step 3: Handle remaining occurrences by inserting smaller character\n            if(arr[i]!=0){\n                for(int j=i-1; j>=0; j--){ // j: Another pointer\n                    if(arr[j]==0) continue;\n                    sb.append( (char)(\'a\'+j) ); // Adding only one character                   \n                    arr[j]--;\n                    i++;\n                    break;\n                }                \n            }            \n        }\n       \n        return sb.toString();  \n    }\n\n\n}\n```
['Counting', 'Java']