Video | O(n) solution keep count char frequency array, with constant space
YouTube solution Intuitionkeep it simple like below steps:-

create an array of 26 size and count the frequency of all characters.
int count[] = new int[26]


w
# YouTube solution https://youtu.be/LGUrExMrW-c\n# Intuition\nkeep it simple like below steps:- \n\n1) create an array of 26 size and count the frequency of all characters.\nint count[] = new int[26]\n\n2) write a method that will give us next smaller char frequency in count array (remeber we are traversing from right to left **why?** we need lexicographical larger chars) \n\n**s = zzzzaaayy ===> frequency of \'z\' is at index = 25** if i call getNext(count, 25) ===> it will return next smaller index that have non zero frequency ==> **24**for \'y\' frequency.\n\n3) now we will start from **rightmost frequency** and then pick **min(repeatLimit, frequency)** times because we can append repeatLimit if frequency is greater than or equal else we need to print whatever the frequency of char.\n\nupdate the remaing frequency count[curr] -= min(repeatLimit, frequency)\n\nnow we have to two scenerios \n**a) if count[curr]>0 ===>** means we can append only one char from next smaller and get back to print again next set from same frequency.\nb) if count[curr]<=0 ==> means we have to make our curr = index of next smaller char frequency.\n now at any time if we found the there is no more next smaller available we have to break the loop and return the resultant string.\n\n\n# Code\n```java []\nclass Solution {\n    public int getNext(int count[], int curr)\n    {\n      if(curr==0)\n      return -1;  \n      while(curr>0)\n      {\n        if(count[curr-1]>0)\n        return curr-1;\n        curr--;\n      } \n      return -1; \n    }\n\n    public String repeatLimitedString(String s, int repeatLimit) {\n    int count[] = new int[26];\n    StringBuilder sb = new StringBuilder();\n    for(char ch : s.toCharArray())\n    count[ch -\'a\']++;\n\n    int curr = getNext(count, 26); \n    \n    while(curr>=0)\n    {\n           int freq = Math.min(repeatLimit , count[curr]);\n                for (int i = 0; i < freq; i++)\n                sb.append((char) (curr + \'a\'));\n                 count[curr] -= freq;\n       if(count[curr]>0)\n       {       \n       int next = getNext(count, curr);\n       if(next<0)\n       break;\n       sb.append((char) (next + \'a\'));\n       count[next]--;\n       } \n       else   \n       curr = getNext(count, curr);     \n    }\n\n     return sb.toString();\n\n        \n    }\n}\n```
['Java']