algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
2 shown · 0/261 solved

Round-Robin Rearrangement

BronzeCommunity Beta
Asked atZoho
Solve problem →
2000ms256MBAdded Aug 20, 2026

You are given a string s of lowercase English letters.

Rearrange it as follows:

  1. List the distinct characters of s in the order of their first occurrence.
  2. Emit rounds: in each round, go through that list in order and output one occurrence of every character that still has copies remaining.
  3. Repeat until all characters are used.

Return the resulting string.

Examples

Example 1

Input: s = "cccabbbbcca"
Output: "cabcabcbcbc"
Explanation: First-occurrence order is c, a, b with counts c×5, a×2, b×4.
Rounds: "cab", "cab" (a is used up), "cb", "cb" (b is used up), "c".
Concatenated: "cabcabcbcbc".

Example 2

Input: s = "zzz"
Output: "zzz"
Explanation: One distinct character; every round emits a single z.

Constraints

  • 1≤1 \leq1≤ s.length ≤105\leq 10^5≤105
  • s consists of lowercase English letters

Details

Solved by4 people
Time limit2000ms
Memory256MB
AddedAug 20, 20263 weeks ago