👉Two Pointer Simple Solution 🧠
🧠 IntuitionWe are tasked with constructing the lexicographically largest string possible from the string s, while ensuring that no character appears more than r
# \uD83E\uDDE0 Intuition\n\nWe are tasked with constructing the lexicographically largest string possible from the string `s`, while ensuring that no character appears more than `repeatLimit` times consecutively.\n\nTo achieve this, we need to:\n- **Maximize lexicographical order**: We prioritize placing the lexicographically largest characters in the result first.\n- **Avoid exceeding the repeat limit**: We need to carefully manage the addition of characters to avoid placing any character more than `repeatLimit` times consecutively.\n\n### Key Insights:\n1. **Sorting in Descending Order**: Sorting the characters of the string in descending order allows us to first try to place the largest characters. This helps in creating the lexicographically largest string.\n2. **Consecutive Occurrences**: We need to ensure that we don\'t violate the constraint that no character appears more than `repeatLimit` times consecutively. If we try to add a character that would exceed this limit, we must skip it and use another character.\n\n# \uD83D\uDE80 Approach\n\n1. **Sort the String**:\n   - We first sort the string `s` in descending order using `sort(s.rbegin(), s.rend())`. This ensures that the largest characters appear first, giving us the lexicographically largest possible result when constructing the string.\n   \n2. **Iterate Over the Sorted String**:\n   - We need to iterate through the sorted string and build the result string, while managing the consecutive occurrences of characters.\n   - For each character in the string:\n     - If it\'s the same as the last character added to the result, we check if it has already appeared `repeatLimit` times consecutively. If it has, we skip it and attempt to add a different character.\n     - If it\'s a different character, we add it directly to the result string.\n\n3. **Handling the Repeat Limit**:\n   - For every character added to the result, we track how many times that character has appeared using a variable `freq`.\n   - If a character appears `repeatLimit` times consecutively, we move on to the next available character, which may involve skipping over repeated characters using a `pointer`.\n\n4. **Pointer Management**:\n   - If we encounter a character that has already appeared `repeatLimit` times consecutively, we use a pointer to find the next distinct character in the sorted string and add that to the result.\n   - The pointer ensures that we don\'t violate the repeat limit and helps us find the next valid character to place.\n\n5. **Final Output**:\n   - After processing all the characters and carefully managing the repeat limit, we return the constructed string as the result.\n\n# \u23F3 Complexity\n\n- **Time complexity**:\n  - Sorting the string takes **O(n log n)**, where `n` is the length of the string `s`.\n  - The iteration through the string takes **O(n)**. Hence, the overall time complexity is dominated by the sorting step, giving a total of **O(n log n)**.\n\n- **Space complexity**:\n  - We use a few variables (e.g., `freq`, `pointer`) and store the result string, which gives a space complexity of **O(n)**, where `n` is the length of the string.\n\n# \uD83D\uDCBB Code\n\n```cpp []\n#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n    string repeatLimitedString(string s, int repeatLimit) {\n        sort(s.rbegin(), s.rend());\n\n        string result;\n        int freq = 1;\n        int pointer = 0;\n\n        for (int i = 0; i < s.size(); ++i) {\n            if (!result.empty() && result.back() == s[i]) {\n                if (freq < repeatLimit) {\n                    result += s[i];\n                    freq++;\n                } else {\n                    pointer = max(pointer, i + 1);\n                    while (pointer < s.size() && s[pointer] == s[i]) {\n                        pointer++;\n                    }\n\n                    if (pointer < s.size()) {\n                        result += s[pointer];\n                        swap(s[i], s[pointer]);\n                        freq = 1;\n                    } else {\n                        break;\n                    }\n                }\n            } else {\n                result += s[i];\n                freq = 1;\n            }\n        }\n\n        return result;\n    }\n};\n```\n```JavaScript []\nclass Solution:\n    def repeatLimitedString(self, s: str, repeatLimit: int) -> str:\n        s = sorted(s, reverse=True)\n        result = []\n        freq = 1\n        pointer = 0\n\n        i = 0\n        while i < len(s):\n            if result and result[-1] == s[i]:\n                if freq < repeatLimit:\n                    result.append(s[i])\n                    freq += 1\n                else:\n                    pointer = max(pointer, i + 1)\n                    while pointer < len(s) and s[pointer] == s[i]:\n                        pointer += 1\n\n                    if pointer < len(s):\n                        result.append(s[pointer])\n                        s[i], s[pointer] = s[pointer], s[i]\n                        freq = 1\n                    else:\n                        break\n            else:\n                result.append(s[i])\n                freq = 1\n\n            i += 1\n\n        return \'\'.join(result)\n\n```\n```Java []\nclass Solution {\n    public String repeatLimitedString(String s, int repeatLimit) {\n        char[] chars = s.toCharArray();\n        Arrays.sort(chars);\n        StringBuilder result = new StringBuilder();\n        \n        int freq = 1;\n        int pointer = chars.length - 1;\n\n        for (int i = chars.length - 1; i >= 0; --i) {\n            if (result.length() > 0 && result.charAt(result.length() - 1) == chars[i]) {\n                if (freq < repeatLimit) {\n                    result.append(chars[i]);\n                    freq++;\n                } else {\n                    while (pointer >= 0 && (chars[pointer] == chars[i] || pointer > i)) {\n                        pointer--;\n                    }\n\n                    if (pointer >= 0) {\n                        result.append(chars[pointer]);\n                        char temp = chars[i];\n                        chars[i] = chars[pointer];\n                        chars[pointer] = temp;\n                        freq = 1;\n                    } else {\n                        break;\n                    }\n                }\n            } else {\n                result.append(chars[i]);\n                freq = 1;\n            }\n        }\n        return result.toString();\n    }\n}\n\n```\n```Python3 []\nclass Solution:\n    def repeatLimitedString(self, s: str, repeatLimit: int) -> str:\n        chars = sorted(s, reverse=True)\n        \n        result = []\n        freq = 1\n        pointer = 0\n        \n        for i in range(len(chars)):\n            if result and result[-1] == chars[i]:\n                if freq < repeatLimit:\n                    result.append(chars[i])\n                    freq += 1\n                else:\n                    pointer = max(pointer, i + 1)\n                    while pointer < len(chars) and chars[pointer] == chars[i]:\n                        pointer += 1\n                    \n                    if pointer < len(chars):\n                        result.append(chars[pointer])\n                        chars[i], chars[pointer] = chars[pointer], chars[i]\n                        freq = 1\n                    else:\n                        break\n            else:\n                result.append(chars[i])\n                freq = 1\n        \n        return \'\'.join(result)\n\n```\n```C# []\nusing System;\nusing System.Linq;\nusing System.Text;\n\npublic class Solution {\n    public string RepeatLimitedString(string s, int repeatLimit) {\n        char[] chars = s.ToCharArray();\n        Array.Sort(chars, (a, b) => b.CompareTo(a));\n\n        StringBuilder result = new StringBuilder();\n        int freq = 1;\n        int pointer = 0;\n\n        for (int i = 0; i < chars.Length; ++i) {\n            if (result.Length > 0 && result[result.Length - 1] == chars[i]) {\n                if (freq < repeatLimit) {\n                    result.Append(chars[i]);\n                    freq++;\n                } else {\n                    pointer = Math.Max(pointer, i + 1);\n                    while (pointer < chars.Length && chars[pointer] == chars[i]) {\n                        pointer++;\n                    }\n\n                    if (pointer < chars.Length) {\n                        result.Append(chars[pointer]);\n                        char temp = chars[i];\n                        chars[i] = chars[pointer];\n                        chars[pointer] = temp;\n                        freq = 1;\n                    } else {\n                        break;\n                    }\n                }\n            } else {\n                result.Append(chars[i]);\n                freq = 1;\n            }\n        }\n\n        return result.ToString();\n    }\n}\n```
['Two Pointers', 'C++', 'Java', 'Python3', 'Ruby', 'JavaScript', 'C#']