🔥🔥🔥🔥O(N) solution 🔥🔥🔥🔥
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    string repeatLimitedString(string s, int repeatLimit) {
        if (s.length() == 1)
            return s;
        sort(s.begin(), s.end(), greater<char>());//use count sort for O(N)
        vector<int> endPos(26, 0);
        int sum = -1;
        for (auto ch : s) {
            sum++;
            endPos[ch - 'a'] = sum;
        }
        int i = 0, j = 1;
        while (j < s.length()) {
            if (s[i] == s[j]) {
                if ((j - i) >= repeatLimit) {
                    if ((endPos[s[j] - 'a'] + 1) < s.length()) {
                        i = j;
                        endPos[s[j] - 'a']++;
                        swap(s[j], s[endPos[s[j] - 'a']]);
                        
                    } else {
                        return s.substr(0, j);
                    }
                }
                j++;
            } else {
                i = j;
                j = i + 1;
            }
        }
        return s;
    }
};
```
['Counting', 'C++']