Skip to content

Commit cfba82f

Browse files
anonimakignacio-chiazzo
authored andcommitted
Added Shuffle_String problem solution
1 parent efa3855 commit cfba82f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
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.
3+
4+
Return the shuffled string.
5+
6+
7+
8+
Example 1:
9+
10+
Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
11+
Output: "leetcode"
12+
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.
13+
14+
Example 2:
15+
16+
Input: s = "abc", indices = [0,1,2]
17+
Output: "abc"
18+
Explanation: After shuffling, each character remains in its position.
19+
20+
*/
21+
22+
/**
23+
* @param {string} s
24+
* @param {number[]} indices
25+
* @return {string}
26+
*/
27+
var restoreString = function(s, indices) {
28+
let arrshuffle = [];
29+
indices.forEach((sindex, index) => arrshuffle[sindex] = s.charAt(index))
30+
return arrshuffle.join('')
31+
};
32+
33+
module.exports.restoreString = restoreString;

0 commit comments

Comments
 (0)