Shuffle String

You are given a string s and an integer array indices of the same length. The character at position i of s belongs at position indices[i] of the answer. Return the rearranged string.

30:00

Shuffle String
easy
Free written solution
Topics
Companies

You are given a string s and an integer array indices of the same length. The character at position i of s belongs at position indices[i] of the answer. Return the rearranged string.

Example 1:
Input: {"s":"codeleet","indices":[4,5,6,7,0,2,1,3]}
Output: leetcode
Constraints:
  • s.length=indices.length=ns.\text{length} = \text{indices.length} = n

  • 1≤n≤1001 \leq n \leq 100

  • s consists of only lowercase English letters.

  • 0≤indices[i]<n0 \leq \text{indices}[i] < n

  • All values of indices are unique.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"codeleet","indices":[4,5,6,7,0,2,1,3]}

Make 8 empty slots. Each letter of s carries a ticket, indices[i], naming the slot it must land in.

s: each letter holds a ticket (its target slot)

0

c
→4

1

o
→5

2

d
→6

3

e
→7

4

l
→0

5

e
→2

6

e
→1

7

t
→3

result slots (0 of 8 filled)

0

1

2

3

4

5

6

7

Variables
VariableValue
n8
DepthFunction Call
Stack empty
0/10