Skip to content

Commit c774c2c

Browse files
authored
Create 1528_Shuffle_String.md
1 parent be9be7d commit c774c2c

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

1528_Shuffle_String.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## 1528. Shuffle String
2+
3+
Given a string s and an integer array indices of the same length.
4+
5+
The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
6+
7+
Return the shuffled string.
8+
9+
10+
11+
Example 1:
12+
13+
14+
Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
15+
Output: "leetcode"
16+
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.
17+
Example 2:
18+
19+
Input: s = "abc", indices = [0,1,2]
20+
Output: "abc"
21+
Explanation: After shuffling, each character remains in its position.
22+
Example 3:
23+
24+
Input: s = "aiohn", indices = [3,1,4,2,0]
25+
Output: "nihao"
26+
Example 4:
27+
28+
Input: s = "aaiougrt", indices = [4,0,2,6,7,3,1,5]
29+
Output: "arigatou"
30+
Example 5:
31+
32+
Input: s = "art", indices = [1,0,2]
33+
Output: "rat"
34+
35+
36+
Constraints:
37+
38+
s.length == indices.length == n
39+
1 <= n <= 100
40+
s contains only lower-case English letters.
41+
0 <= indices[i] < n
42+
All values of indices are unique (i.e. indices is a permutation of the integers from 0 to n - 1).
43+
44+
```python
45+
def restoreString(self, s: str, indices: List[int]) -> str:
46+
result = [''] * len(indices)
47+
for i, c in enumerate(s):
48+
index = indices[i]
49+
result[index] = c
50+
51+
return "".join(result)
52+
```
53+
54+
55+
```
56+
Runtime: 52 ms, faster than 77.27% of Python3 online submissions for Shuffle String.
57+
Memory Usage: 14.2 MB, less than 100.00% of Python3 online submissions for Shuffle String.
58+
```

0 commit comments

Comments
 (0)