Shuffle String
Real-world version: Reassembling The Scrambled CodeThe statement here is our own: the same inputs, outputs and technique.
A ticket code came off a faulty printer with its characters out of order, but every character was tagged with the position it belongs in.
You are given code, the scrambled string, and slots of the same length. The character code[i] belongs at position slots[i] of the restored code, with positions counted from 0.
Return the restored code.
Examples
Example 1
- Input
- code = "dcba", slots = [3, 2, 1, 0]
- Output
- "abcd"
The "d" is tagged for position 3, the "c" for position 2, the "b" for position 1 and the "a" for position 0, which spells "abcd".
Example 2
- Input
- code = "wxy", slots = [1, 2, 0]
- Output
- "ywx"
The "w" goes to position 1, the "x" to position 2 and the "y" to position 0, giving "ywx".
Example 3
- Input
- code = "pill", slots = [2, 3, 0, 1]
- Output
- "llpi"
Positions 2 and 3 take the "p" and the "i", while positions 0 and 1 take the two "l" characters, spelling "llpi".
Constraints
- 1 <= code.length <= 100
- slots.length == code.length
- 0 <= slots[i] <= 99
- code consists of lowercase English letters only.
- The values in slots are exactly the positions 0 through code.length - 1, each used once.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def restore_code(code: str, slots: list[int]) -> str:public String restoreCode(String code, int[] slots)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Longest Common PrefixReal-world version: Shared Crate CodeEasy
- Shortest Word DistanceReal-world version: Closest Station Calls in the Announcement LogEasy
- Verifying an Alien DictionaryReal-world version: Catalogue Order Under a Custom Glyph SequenceEasy
- Find Common CharactersReal-world version: Tiles Every Rack Can SupplyEasy