Skip to content

Commit 56a25f4

Browse files
committed
feat: add solutions to lc problem: No.1528
1 parent 198bf6f commit 56a25f4

File tree

7 files changed

+109
-27
lines changed

7 files changed

+109
-27
lines changed

solution/1500-1599/1528.Shuffle String/README.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ tags:
6363

6464
<!-- solution:start -->
6565

66-
### 方法一
66+
### 方法一:模拟
67+
68+
我们创建一个与字符串长度相同的字符数组或字符串 $\textit{ans}$,然后遍历字符串 $\textit{s}$,将每个字符 $\textit{s}[i]$ 放置到 $\textit{ans}$ 的 $\textit{indices}[i]$ 位置上。最后将字符数组或字符串 $\textit{ans}$ 拼接成最终结果并返回。
69+
70+
时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是字符串的长度。
6771

6872
<!-- tabs:start -->
6973

@@ -72,10 +76,10 @@ tags:
7276
```python
7377
class Solution:
7478
def restoreString(self, s: str, indices: List[int]) -> str:
75-
ans = [0] * len(s)
76-
for i, c in enumerate(s):
77-
ans[indices[i]] = c
78-
return ''.join(ans)
79+
ans = [None] * len(s)
80+
for c, j in zip(s, indices):
81+
ans[j] = c
82+
return "".join(ans)
7983
```
8084

8185
#### Java
@@ -88,7 +92,7 @@ class Solution {
8892
for (int i = 0; i < n; ++i) {
8993
ans[indices[i]] = s.charAt(i);
9094
}
91-
return String.valueOf(ans);
95+
return new String(ans);
9296
}
9397
}
9498
```
@@ -121,6 +125,34 @@ func restoreString(s string, indices []int) string {
121125
}
122126
```
123127

128+
#### TypeScript
129+
130+
```ts
131+
function restoreString(s: string, indices: number[]): string {
132+
const ans: string[] = [];
133+
for (let i = 0; i < s.length; i++) {
134+
ans[indices[i]] = s[i];
135+
}
136+
return ans.join('');
137+
}
138+
```
139+
140+
#### Rust
141+
142+
```rust
143+
impl Solution {
144+
pub fn restore_string(s: String, indices: Vec<i32>) -> String {
145+
let n = s.len();
146+
let mut ans = vec![' '; n];
147+
let chars: Vec<char> = s.chars().collect();
148+
for i in 0..n {
149+
ans[indices[i] as usize] = chars[i];
150+
}
151+
ans.iter().collect()
152+
}
153+
}
154+
```
155+
124156
#### JavaScript
125157

126158
```js
@@ -130,11 +162,11 @@ func restoreString(s string, indices []int) string {
130162
* @return {string}
131163
*/
132164
var restoreString = function (s, indices) {
133-
let rs = [];
165+
const ans = [];
134166
for (let i = 0; i < s.length; i++) {
135-
rs[indices[i]] = s[i];
167+
ans[indices[i]] = s[i];
136168
}
137-
return rs.join('');
169+
return ans.join('');
138170
};
139171
```
140172

solution/1500-1599/1528.Shuffle String/README_EN.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Simulation
61+
62+
We create a character array or string $\textit{ans}$ of the same length as the input string, then iterate through the string $\textit{s}$ and place each character $\textit{s}[i]$ at position $\textit{indices}[i]$ in $\textit{ans}$. Finally, we join the character array or string $\textit{ans}$ to form the final result and return it.
63+
64+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the string.
6165

6266
<!-- tabs:start -->
6367

@@ -66,10 +70,10 @@ tags:
6670
```python
6771
class Solution:
6872
def restoreString(self, s: str, indices: List[int]) -> str:
69-
ans = [0] * len(s)
70-
for i, c in enumerate(s):
71-
ans[indices[i]] = c
72-
return ''.join(ans)
73+
ans = [None] * len(s)
74+
for c, j in zip(s, indices):
75+
ans[j] = c
76+
return "".join(ans)
7377
```
7478

7579
#### Java
@@ -82,7 +86,7 @@ class Solution {
8286
for (int i = 0; i < n; ++i) {
8387
ans[indices[i]] = s.charAt(i);
8488
}
85-
return String.valueOf(ans);
89+
return new String(ans);
8690
}
8791
}
8892
```
@@ -115,6 +119,34 @@ func restoreString(s string, indices []int) string {
115119
}
116120
```
117121

122+
#### TypeScript
123+
124+
```ts
125+
function restoreString(s: string, indices: number[]): string {
126+
const ans: string[] = [];
127+
for (let i = 0; i < s.length; i++) {
128+
ans[indices[i]] = s[i];
129+
}
130+
return ans.join('');
131+
}
132+
```
133+
134+
#### Rust
135+
136+
```rust
137+
impl Solution {
138+
pub fn restore_string(s: String, indices: Vec<i32>) -> String {
139+
let n = s.len();
140+
let mut ans = vec![' '; n];
141+
let chars: Vec<char> = s.chars().collect();
142+
for i in 0..n {
143+
ans[indices[i] as usize] = chars[i];
144+
}
145+
ans.iter().collect()
146+
}
147+
}
148+
```
149+
118150
#### JavaScript
119151

120152
```js
@@ -124,11 +156,11 @@ func restoreString(s string, indices []int) string {
124156
* @return {string}
125157
*/
126158
var restoreString = function (s, indices) {
127-
let rs = [];
159+
const ans = [];
128160
for (let i = 0; i < s.length; i++) {
129-
rs[indices[i]] = s[i];
161+
ans[indices[i]] = s[i];
130162
}
131-
return rs.join('');
163+
return ans.join('');
132164
};
133165
```
134166

solution/1500-1599/1528.Shuffle String/Solution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ public String restoreString(String s, int[] indices) {
55
for (int i = 0; i < n; ++i) {
66
ans[indices[i]] = s.charAt(i);
77
}
8-
return String.valueOf(ans);
8+
return new String(ans);
99
}
10-
}
10+
}

solution/1500-1599/1528.Shuffle String/Solution.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* @return {string}
55
*/
66
var restoreString = function (s, indices) {
7-
let rs = [];
7+
const ans = [];
88
for (let i = 0; i < s.length; i++) {
9-
rs[indices[i]] = s[i];
9+
ans[indices[i]] = s[i];
1010
}
11-
return rs.join('');
11+
return ans.join('');
1212
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def restoreString(self, s: str, indices: List[int]) -> str:
3-
ans = [0] * len(s)
4-
for i, c in enumerate(s):
5-
ans[indices[i]] = c
6-
return ''.join(ans)
3+
ans = [None] * len(s)
4+
for c, j in zip(s, indices):
5+
ans[j] = c
6+
return "".join(ans)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn restore_string(s: String, indices: Vec<i32>) -> String {
3+
let n = s.len();
4+
let mut ans = vec![' '; n];
5+
let chars: Vec<char> = s.chars().collect();
6+
for i in 0..n {
7+
ans[indices[i] as usize] = chars[i];
8+
}
9+
ans.iter().collect()
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function restoreString(s: string, indices: number[]): string {
2+
const ans: string[] = [];
3+
for (let i = 0; i < s.length; i++) {
4+
ans[indices[i]] = s[i];
5+
}
6+
return ans.join('');
7+
}

0 commit comments

Comments
 (0)