Problem · String

Construct WDL String

EasyTiktokOA
See Tiktok hiring insights

You are given a string inputStr containing only the letters W, D, and L. Construct a new string from the characters of inputStr using the following algorithm.

  1. Begin with an empty string output = "".
  2. If inputStr contains a W, remove any one W from inputStr and append W to the end of output.
  3. If inputStr contains a D, remove any one D from inputStr and append D to the end of output.
  4. If inputStr contains an L, remove any one L from inputStr and append L to the end of output.
  5. If inputStr is empty, stop. Otherwise, repeat from the W step.

Return output after the algorithm is complete.

Examples
01 · Example 1
inputStr = "LDWDL"
return = "WDLDL"
The first pass appends W, D, and L, leaving one D and one L. The second pass appends D and L, so the final string is "WDLDL".
02 · Example 2
inputStr = "LLDWW"
return = "WDLWL"
There are two W letters, one D letter, and two L letters. The algorithm appends W, D, L in the first pass, then W and L in the second pass.
Constraints
  • inputStr contains only the letters W, D, and L.
  • A solution with time complexity no worse than O(inputStr.length2) fits within the execution time limit.
More Tiktok problems
drafts saved locally
public String constructWdlString(String inputStr) {
  // write your code here
}
inputStr"LDWDL"
expected"WDLDL"
sign in to submit