✅Simple Solution | Beginner Friendly | Easy Approach with Explanation✅
IntuitionThe goal is to construct the largest lexicographical string such that no character appears more than repeatLimit times consecutively.To achieve this, w
# Intuition\nThe goal is to construct the largest lexicographical string such that no character appears more than repeatLimit times consecutively.  \n\nTo achieve this, we can:\n1. Use a frequency count of each character in the string.\n2. Start from the largest character (lexicographically) and add it to the result as many times as allowed.\n3. If a character exceeds the limit, we add the next largest character to "break the streak."\n\n# Approach\n1. *Count frequencies* of all characters using a frequency array (size 26 for lowercase letters).\n2. Start iterating from the largest character (z -> a) using two pointers:  \n   - i for the current largest character.\n   - j for the next largest character (to use when the limit is exceeded).\n3. For each character:\n   - Add it to the result min(repeatLimit, frequency) times.\n   - If there are still characters left, find the next available character (j) and add it once.\n4. Repeat until we process all characters or cannot add any more.\n\n\n# Code\n```javascript []\n/**\n * @param {string} s\n * @param {number} repeatLimit\n * @return {string}\n */\nvar repeatLimitedString = function(s, repeatLimit) {\n    const freq = Array(26).fill(0);\n    for (const char of s) {\n        freq[char.charCodeAt(0) - \'a\'.charCodeAt(0)]++;\n    }\n\n    let result = [];\n    let i = 25;\n    let j = 24;\n\n    while (i >= 0) {\n        if (freq[i] === 0) {\n            i--;\n            continue;\n        }\n\n        let count = Math.min(repeatLimit, freq[i]);\n        result.push(String.fromCharCode(97 + i).repeat(count));\n        freq[i] -= count;\n\n        if (freq[i] > 0) {\n            while (j >= 0 && (freq[j] === 0 || j === i)) {\n                j--;\n            }\n\n            if (j < 0) break;\n\n            result.push(String.fromCharCode(97 + j));\n            freq[j]--;\n        } else {\n            i--;\n        }\n    }\n\n    return result.join(\'\');\n};\n```\n\n``` typescript []\nfunction repeatLimitedString(s: string, repeatLimit: number): string {\n    const freq: number[] = new Array(26).fill(0);\n    for (const char of s) {\n        freq[char.charCodeAt(0) - \'a\'.charCodeAt(0)]++;\n    }\n\n    let result: string[] = [];\n    let i: number = 25;\n    let j: number = 24;\n\n    while (i >= 0) {\n        if (freq[i] === 0) {\n            i--;\n            continue;\n        }\n\n        let count: number = Math.min(repeatLimit, freq[i]);\n        result.push(String.fromCharCode(97 + i).repeat(count));\n        freq[i] -= count;\n\n        if (freq[i] > 0) {\n            while (j >= 0 && (freq[j] === 0 || j === i)) {\n                j--;\n            }\n\n            if (j < 0) break;\n\n            result.push(String.fromCharCode(97 + j));\n            freq[j]--;\n        } else {\n            i--;\n        }\n    }\n\n    return result.join(\'\');\n}
['TypeScript', 'JavaScript']