Shuffle String Problem
Description
LeetCode Problem 1528.
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 i^th position moves to indices[i] in the shuffled string.
Return the shuffled string.
Example 1:

Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
Output: "leetcode"
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.
Example 2:
Input: s = "abc", indices = [0,1,2]
Output: "abc"
Explanation: After shuffling, each character remains in its position.
Constraints:
s.length == indices.length == n1 <= n <= 100sconsists of only lowercase English letters.0 <= indices[i] < n- All values of
indicesare unique.
Solution
Approach: Place Each Character Directly
The statement already tells us where each character goes: s[i] belongs at position indices[i] of the result. So make a result buffer of the same length and, for every i, write s[i] into slot indices[i].
Because the values of indices are all different and cover 0 to n - 1, every slot is written exactly once and none is left empty. Note the direction: we write ans[indices[i]] = s[i], not ans[i] = s[indices[i]]; the second one would apply the inverse shuffle (for "codeleet" it would not give "leetcode").
Complexity: O(n) time and O(n) space for the result.
class Solution:
def restoreString(self, s: str, indices: List[int]) -> str:
ans = [""] * len(s)
for c, j in zip(s, indices):
ans[j] = c # s[i] moves to position indices[i]
return "".join(ans)