Shuffle String - Problem

You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.

Return the shuffled string.

Input & Output

Example 1 — Basic Shuffling
$ Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
Output: "leetcode"
💡 Note: Characters are rearranged: s[0]='c' goes to indices[0]=4, s[1]='o' goes to indices[1]=5, etc. Result: "leetcode"
Example 2 — Short String
$ Input: s = "abc", indices = [0,1,2]
Output: "abc"
💡 Note: Each character stays in the same position: 'a' at 0, 'b' at 1, 'c' at 2
Example 3 — Complete Reversal
$ Input: s = "aiohn", indices = [3,1,4,2,0]
Output: "nihao"
💡 Note: s[0]='a' → pos 3, s[1]='i' → pos 1, s[2]='o' → pos 4, s[3]='h' → pos 2, s[4]='n' → pos 0

Constraints

  • s.length == indices.length
  • 1 ≤ s.length ≤ 100
  • s contains only lowercase English letters
  • 0 ≤ indices[i] < s.length
  • All values of indices are unique

Visualization

Tap to expand
Shuffle String: Rearrange characters by indicesInput String:codeleetIndices:45670213Result:leetcode"leetcode"
Understanding the Visualization
1
Input
String 'codeleet' and indices [4,5,6,7,0,2,1,3]
2
Mapping
Each character moves to its target position
3
Output
Shuffled string 'leetcode'
Key Takeaway
🎯 Key Insight: Each character has a predetermined target position - just place them directly without complex rearrangement logic
Asked in
Microsoft 15 Amazon 12 Google 8
72.8K Views
Medium Frequency
~8 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen