Skip to main content
Back to problems
#1528
Easy Algorithms

Shuffle string

Array String
85.4% acceptance
Feb 25, 2026
2933
544
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.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn restore_string(s: String, indices: Vec<i32>) -> String {
    let bytes = s.as_bytes();
    let mut result = vec![0u8; bytes.len()];
    for (i, &idx) in indices.iter().enumerate() {
      result[idx as usize] = bytes[i];
    }
    String::from_utf8(result).unwrap()
  }
}