Problem · String

Round-Robin WDL Order

Learn this problem
EasyCapital OneOA

Problem statement

Given a string sequence containing only W, D, and L, build a reordered string by consuming the available characters cyclically in this order:

  1. Append W if any W characters remain.
  2. Append D if any D characters remain.
  3. Append L if any L characters 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

reorderWdl(sequence: String) → String

Examples

Example 1

sequence = "WWWLLDDLD"return = "WDLWDLWDL"

The input has three copies of each character. Each complete W, D, L cycle consumes one of each, producing WDLWDLWDL.

Example 2

sequence = "WLDDL"return = "WDLDL"

The first cycle produces WDL. No W remains, so the next cycle skips W and appends D followed by L, producing WDLDL.

Example 3

sequence = "WWWWLDDL"return = "WDLWDLWW"

Two complete cycles consume two copies of each character and produce WDLWDL. Only two W characters remain, so they are appended in the next two cycles, producing WDLWDLWW.

Constraints

  • Every character in sequence is W, D, or L.

More Capital One problems

drafts saved locally
public String reorderWdl(String sequence) {
  // Write your code here.
}
sequence"WWWLLDDLD"
expected"WDLWDLWDL"
checking account