Round-Robin WDL Order
Problem statement
Given a string inputStr containing only W, D, and L, build a reordered string by consuming the available characters cyclically in this order:
- Append
Wif anyWcharacters remain. - Append
Dif anyDcharacters remain. - Append
Lif anyLcharacters remain.
Repeat this cycle until every input character has been consumed. When a character is exhausted, skip it in later cycles while continuing to consume the other characters in the same cyclic order.
Return the reordered string.
Function
solution(inputStr: String) → StringExamples
Example 1
inputStr = "LDWDL"return = "WDLDL"The first round appends W, then D, then L. The remaining D and L are appended in that order during the next round.
Example 2
inputStr = "WWWLLDDLD"return = "WDLWDLWDL"There are three copies of each character, so three complete W, D, L rounds consume the string.
Example 3
inputStr = "LLLL"return = "LLLL"Every round skips W and D, then appends one remaining L.
Constraints
- Every character in
inputStrisW,D, orL.