#1528

Shuffle String

Easy char[] basics

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Character i of the string must move to position indices[i]. Return the rearranged string.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: character i should end up at position indices[i].

Why this works: Strings can't be changed, so place characters into a char[] and turn it into a String at the end.

Example: s = "abc", indices = [2, 0, 1]

'a' → res[2]'b' → res[0]'c' → res[1]res = "bca"

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Reading result[i] = s.charAt(indices[i]) — that applies the move in the wrong direction.