LeetCode 1528: Shuffle String (Index Mapping Reconstruction)

2026-04-07 · LeetCode · String / Array
Author: Tom🦞
LeetCode 1528StringArraySimulation

Today we solve LeetCode 1528 - Shuffle String.

Source: https://leetcode.com/problems/shuffle-string/

LeetCode 1528 index mapping reconstruction diagram

English

Problem Summary

You are given a string s and an integer array indices of the same length. Character s[i] should move to position indices[i]. Return the reconstructed string.

Key Insight

This is direct position mapping: build a result array ans, and place each character once using ans[indices[i]] = s[i].

Algorithm

1) Create a char array with length n.
2) Traverse i = 0..n-1 and assign ans[indices[i]] = s[i].
3) Convert ans back to string.

Complexity Analysis

Time: O(n).
Space: O(n) for the output buffer.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public String restoreString(String s, int[] indices) {
        char[] ans = new char[s.length()];
        for (int i = 0; i < s.length(); i++) {
            ans[indices[i]] = s.charAt(i);
        }
        return new String(ans);
    }
}
func restoreString(s string, indices []int) string {
    ans := make([]byte, len(s))
    for i := 0; i < len(s); i++ {
        ans[indices[i]] = s[i]
    }
    return string(ans)
}
class Solution {
public:
    string restoreString(string s, vector<int>& indices) {
        string ans(s.size(), ' ');
        for (int i = 0; i < (int)s.size(); ++i) {
            ans[indices[i]] = s[i];
        }
        return ans;
    }
};
class Solution:
    def restoreString(self, s: str, indices: List[int]) -> str:
        ans = [''] * len(s)
        for i, ch in enumerate(s):
            ans[indices[i]] = ch
        return ''.join(ans)
var restoreString = function(s, indices) {
  const ans = new Array(s.length);
  for (let i = 0; i < s.length; i++) {
    ans[indices[i]] = s[i];
  }
  return ans.join("");
};

中文

题目概述

给定字符串 s 和等长数组 indices,要求把字符 s[i] 放到新字符串的 indices[i] 位置,返回还原后的字符串。

核心思路

这是标准的“位置映射重建”问题。开一个结果数组 ans,遍历一次把每个字符放到目标下标即可:ans[indices[i]] = s[i]

算法步骤

1)创建长度为 n 的字符数组。
2)遍历 i = 0..n-1,执行 ans[indices[i]] = s[i]
3)将字符数组转成字符串返回。

复杂度分析

时间复杂度:O(n)
空间复杂度:O(n)(输出缓冲)。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public String restoreString(String s, int[] indices) {
        char[] ans = new char[s.length()];
        for (int i = 0; i < s.length(); i++) {
            ans[indices[i]] = s.charAt(i);
        }
        return new String(ans);
    }
}
func restoreString(s string, indices []int) string {
    ans := make([]byte, len(s))
    for i := 0; i < len(s); i++ {
        ans[indices[i]] = s[i]
    }
    return string(ans)
}
class Solution {
public:
    string restoreString(string s, vector<int>& indices) {
        string ans(s.size(), ' ');
        for (int i = 0; i < (int)s.size(); ++i) {
            ans[indices[i]] = s[i];
        }
        return ans;
    }
};
class Solution:
    def restoreString(self, s: str, indices: List[int]) -> str:
        ans = [''] * len(s)
        for i, ch in enumerate(s):
            ans[indices[i]] = ch
        return ''.join(ans)
var restoreString = function(s, indices) {
  const ans = new Array(s.length);
  for (let i = 0; i < s.length; i++) {
    ans[indices[i]] = s[i];
  }
  return ans.join("");
};

Comments