Construct String With Repeat Limit - Problem
You are given a string s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no letter appears more than repeatLimit times in a row.
You do not have to use all characters from s.
Return the lexicographically largest repeatLimitedString possible.
A string a is lexicographically larger than a string b if in the first position where a and b differ, string a has a letter that appears later in the alphabet than the corresponding letter in b. If the first min(a.length, b.length) characters do not differ, then the longer string is the lexicographically larger one.
Input & Output
Example 1 — Basic Case
$
Input:
s = "cczazcc", repeatLimit = 3
›
Output:
"zzcccacc"
💡 Note:
Start with largest character 'z' (appears 2 times), then 'c' (3 times max), then use 'a' as separator, then remaining 'c'.
Example 2 — Repeat Limit Hit
$
Input:
s = "aababab", repeatLimit = 2
›
Output:
"bbabaa"
💡 Note:
Use 'b' twice, then 'a' as separator, then 'b' once, then remaining 'a' characters.
Example 3 — Single Character Type
$
Input:
s = "aaaa", repeatLimit = 2
›
Output:
"aa"
💡 Note:
Only one character type available, use up to repeat limit and stop (no separator available).
Constraints
- 1 ≤ s.length ≤ 105
- 1 ≤ repeatLimit ≤ 104
- s consists of lowercase English letters.
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
String "cczazcc" with repeatLimit = 3
2
Greedy Construction
Use largest characters first, respecting repeat limit
3
Final Result
Lexicographically largest valid string
Key Takeaway
🎯 Key Insight: Always use the largest available character up to the repeat limit, then use the next largest as a separator when needed.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code