Mister-Hope

1528. Shuffle StringMister-HopeJavaJavaScriptTypeScript

Java

/*
 * Runtime: 1 ms, faster than 99.95% of Java online submissions for Shuffle String.
 *
 * Memory Usage: 39.4 MB, less than 7.86% of Java online submissions for Shuffle String.
 */

class Solution {
  public String restoreString(String s, int[] indices) {
    char[] chars = new char[s.length()];

    for (int i = 0; i < indices.length; i++) chars[indices[i]] = s.charAt(i);

    return new String(chars);
  }
}

JavaScript

/*
 * Runtime: 84 ms, faster than 87.24% of JavaScript online submissions for Shuffle String.
 *
 * Memory Usage: 40.3 MB, less than 7.04% of JavaScript online submissions for Shuffle String.
 */
const restoreString = (s, indices) => {
  const result = [];

  for (let i = 0; i < indices.length; i++) result[indices[i]] = s.charAt(i);

  return result.join("");
};

TypeScript

/*
 * Runtime: 92 ms, faster than 72.15% of TypeScript online submissions for Shuffle String.
 *
 * Memory Usage: 40.4 MB, less than 35.44% of TypeScript online submissions for Shuffle String.
 */
const restoreString = (s: string, indices: number[]): string => {
  const result: string[] = [];

  for (let i = 0; i < indices.length; i++) result[indices[i]] = s.charAt(i);

  return result.join("");
};