-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShuffleString.php
More file actions
38 lines (36 loc) · 1013 Bytes
/
Copy pathShuffleString.php
File metadata and controls
38 lines (36 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
namespace App;
/**
* Shuffle String
*
* 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.
*
* https://leetcode.com/problems/shuffle-string
*/
class ShuffleString
{
/**
* @param string $str
* @param array<array-key, int> $indices
* @return string
*/
public function restoreString(string $str, array $indices): string
{
$strNew = $str;
for ($i = 0; $i < count($indices); $i++) {
$strNew[$indices[$i]] = $str[$i];
}
return $strNew;
}
}