All problems
0764EasyArrayString

Reassembling The Scrambled Code

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1528Shuffle String

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def restore_code(code: str, slots: list[int]) -> str:
Java
public String restoreCode(String code, int[] slots)
September 7
Apply