Shuffle String

easy string array simulation

Problem

You are given a string s and an integer array indices of the same length. The string is shuffled so that the character originally at position i moves to position indices[i] in the result. Return the shuffled string.

Inputs = "codeleet", indices = [4,5,6,7,0,2,1,3]
Output"leetcode"
Character 'c' (i=0) goes to index 4, 'o' (i=1) to index 5, and so on, spelling "leetcode".
Inputs = "abc", indices = [0,1,2]
Output"abc"
Every character keeps its position, so the string is unchanged.

def restore_string(s, indices):
    n = len(s)
    result = [""] * n
    for i in range(n):
        result[indices[i]] = s[i]
    return "".join(result)
function restoreString(s, indices) {
  const n = s.length;
  const result = new Array(n);
  for (let i = 0; i < n; i++) {
    result[indices[i]] = s[i];
  }
  return result.join("");
}
String restoreString(String s, int[] indices) {
    int n = s.length();
    char[] result = new char[n];
    for (int i = 0; i < n; i++) {
        result[indices[i]] = s.charAt(i);
    }
    return new String(result);
}
string restoreString(string s, vector<int>& indices) {
    int n = s.size();
    string result(n, ' ');
    for (int i = 0; i < n; i++) {
        result[indices[i]] = s[i];
    }
    return result;
}
Time: O(n) Space: O(n)