Greedy
Count all characters in 26 buckets (\'a\' to \'z\'). \n2. While we have characters left:\n\t- Find the largest bucket i that still has characters.\n\t- Take up 
1. Count all characters in 26 buckets (\'a\' to \'z\'). \n2. While we have characters left:\n\t- Find the largest bucket `i` that still has characters.\n\t- Take up to limit characters from bucket `i`.\n\t- Find the second largest bucket `j` that has characters.\n\t- Pick 1 character from bucket `j`.\n\nCaveats:\n- The largest bucket `i` could be the same as the second largest one `j` from the previous iteration.\n\t- So we should pick one less character from there.\n- Make sure `i` and `j` only go one direction (down) for the performance.\n\n**C++**\n```cpp\nstring repeatLimitedString(string s, int lim) {\n    string res;\n    int cnt[26] = {};\n    for (auto ch : s)\n        ++cnt[ch - \'a\'];\n    for (int i = 25, j = 26; j >= 0; ) {\n        while (i >= 0 && cnt[i] == 0)\n            --i;\n        if (i >= 0) {\n            res += string(min(lim - (i == j), cnt[i]), \'a\' + i);\n            cnt[i] -= min(lim - (i == j), cnt[i]);\n        }\n        j = min(i - 1, j);\n        while (j >= 0 && cnt[j] == 0)\n            --j;        \n        if (j >=0) {\n            res += string(1, \'a\' + j);\n            --cnt[j];\n        }\n    }\n    return res;\n}\n```
[]