My Java solution with comments
The approach is to add as many copies of the "higher" characters as possible, up to repeatLimit. Then, add only 1 copy of the next highest character as a separa
The approach is to add as many copies of the "higher" characters as possible, up to repeatLimit. Then, add only 1 copy of the next highest character as a separator, and "go back" to the previous character.\n\nFor example, with ```s = "zzzzyy"``` and ```repeatLimit = 2```, we:\n1) Add 2 copies of \'z\'. Since we still have 2 copies remaining that we want to use later, we set ```goBack = 25```.\n2) Since ```goBack != 0```, add only 1 copy of \'y\'. Set ```i = 26``` so that on next loop iteration ```i = 25``` again, and we can add more \'z\'.\n3) Add 2 more copies of \'z\'.\n4) Add final \'y\', which produces \'zzyzzy\'.\n\n```\nclass Solution {\n    public String repeatLimitedString(String s, int repeatLimit) {\n\t    // count number of each character\n        int[] counts = new int[26];\n        for (int i = 0; i < s.length(); i++) {\n            counts[s.charAt(i) - \'a\']++;\n        }\n        \n        StringBuilder sb = new StringBuilder();\n        \n\t\t// if we hit repeatLimit for a character, we can "go back"\n\t\t// to it later after adding some lower character\n        int goBack = 0;\n\t\t\n        for (int i = 25; i >= 0; i--) { // z -> a\n            if (counts[i] > 0) {\n                char c = (char)(\'a\' + i);\n\t\t\t\t\n                if (goBack == 0) {\n\t\t\t\t\t// append as many copies as we have, up to repeatLimit\n                    for (int j = 0; j < repeatLimit && counts[i] > 0; j++) {\n                        sb.append(c);\n                        counts[i]--;\n                    }\n\t\t\t\t\t// if we still have more available then we hit repeatLimit, and we\'ll need\n\t\t\t\t\t// to add some lower character before we can add more of this one\n                    if (counts[i] > 0) {\n                        goBack = i;\n                    }\n                } else {\n\t\t\t\t\t// when goBack is not 0, we want to append only 1 copy of the next lower\n\t\t\t\t\t// character, then go back to the higher character by re-setting i to goBack+1\n                    sb.append(c);\n                    counts[i]--;\n                    i = goBack + 1;\n                    goBack = 0;\n                }\n            }\n        }\n        \n        return sb.toString();\n    }\n}\n```
['Java']