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.
- Begin with an empty string
output = "". - If
inputStrcontains aW, remove any oneWfrominputStrand appendWto the end ofoutput. - If
inputStrcontains aD, remove any oneDfrominputStrand appendDto the end ofoutput. - If
inputStrcontains anL, remove any oneLfrominputStrand appendLto the end ofoutput. - If
inputStris empty, stop. Otherwise, repeat from theWstep.
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
inputStrcontains only the lettersW,D, andL.- A solution with time complexity no worse than
O(inputStr.length2)fits within the execution time limit.
More Tiktok problems
- Check Even-Position MonotonicityOA · Seen Jun 2026
- Count Alternating Tile GroupsOA · Seen Jun 2026
- Count Even-Digit NumbersOA · Seen Jun 2026
- Inventory Discount TrackerOA · Seen Jun 2026
- Find Sum PairsOA · Seen Jun 2026
- LRU Cache with TTL ExpirationONSITE INTERVIEW · Seen May 2026
- Maximum Candies with At Most Two Types in a LineONSITE INTERVIEW · Seen May 2026
- Top-K KOLs by Total LikesONSITE INTERVIEW · Seen May 2026
public String constructWdlString(String inputStr) {
// write your code here
}
inputStr"LDWDL"
expected"WDLDL"
sign in to submit