Show problems as
Classic problem #1528EasyArrayString

Shuffle String

Real-world version: Reassembling The Scrambled Code
Tracked in this browser only
Read Shuffle String on LeetCode(opens in a new tab)

The 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.

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