Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 41 additions & 9 deletions solution/1500-1599/1528.Shuffle String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们创建一个与字符串长度相同的字符数组或字符串 $\textit{ans}$,然后遍历字符串 $\textit{s}$,将每个字符 $\textit{s}[i]$ 放置到 $\textit{ans}$ 的 $\textit{indices}[i]$ 位置上。最后将字符数组或字符串 $\textit{ans}$ 拼接成最终结果并返回。

时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是字符串的长度。

<!-- tabs:start -->

Expand All @@ -72,10 +76,10 @@ tags:
```python
class Solution:
def restoreString(self, s: str, indices: List[int]) -> str:
ans = [0] * len(s)
for i, c in enumerate(s):
ans[indices[i]] = c
return ''.join(ans)
ans = [None] * len(s)
for c, j in zip(s, indices):
ans[j] = c
return "".join(ans)
```

#### Java
Expand All @@ -88,7 +92,7 @@ class Solution {
for (int i = 0; i < n; ++i) {
ans[indices[i]] = s.charAt(i);
}
return String.valueOf(ans);
return new String(ans);
}
}
```
Expand Down Expand Up @@ -121,6 +125,34 @@ func restoreString(s string, indices []int) string {
}
```

#### TypeScript

```ts
function restoreString(s: string, indices: number[]): string {
const ans: string[] = [];
for (let i = 0; i < s.length; i++) {
ans[indices[i]] = s[i];
}
return ans.join('');
}
```

#### Rust

```rust
impl Solution {
pub fn restore_string(s: String, indices: Vec<i32>) -> String {
let n = s.len();
let mut ans = vec![' '; n];
let chars: Vec<char> = s.chars().collect();
for i in 0..n {
ans[indices[i] as usize] = chars[i];
}
ans.iter().collect()
}
}
```

#### JavaScript

```js
Expand All @@ -130,11 +162,11 @@ func restoreString(s string, indices []int) string {
* @return {string}
*/
var restoreString = function (s, indices) {
let rs = [];
const ans = [];
for (let i = 0; i < s.length; i++) {
rs[indices[i]] = s[i];
ans[indices[i]] = s[i];
}
return rs.join('');
return ans.join('');
};
```

Expand Down
50 changes: 41 additions & 9 deletions solution/1500-1599/1528.Shuffle String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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.

The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the string.

<!-- tabs:start -->

Expand All @@ -66,10 +70,10 @@ tags:
```python
class Solution:
def restoreString(self, s: str, indices: List[int]) -> str:
ans = [0] * len(s)
for i, c in enumerate(s):
ans[indices[i]] = c
return ''.join(ans)
ans = [None] * len(s)
for c, j in zip(s, indices):
ans[j] = c
return "".join(ans)
```

#### Java
Expand All @@ -82,7 +86,7 @@ class Solution {
for (int i = 0; i < n; ++i) {
ans[indices[i]] = s.charAt(i);
}
return String.valueOf(ans);
return new String(ans);
}
}
```
Expand Down Expand Up @@ -115,6 +119,34 @@ func restoreString(s string, indices []int) string {
}
```

#### TypeScript

```ts
function restoreString(s: string, indices: number[]): string {
const ans: string[] = [];
for (let i = 0; i < s.length; i++) {
ans[indices[i]] = s[i];
}
return ans.join('');
}
```

#### Rust

```rust
impl Solution {
pub fn restore_string(s: String, indices: Vec<i32>) -> String {
let n = s.len();
let mut ans = vec![' '; n];
let chars: Vec<char> = s.chars().collect();
for i in 0..n {
ans[indices[i] as usize] = chars[i];
}
ans.iter().collect()
}
}
```

#### JavaScript

```js
Expand All @@ -124,11 +156,11 @@ func restoreString(s string, indices []int) string {
* @return {string}
*/
var restoreString = function (s, indices) {
let rs = [];
const ans = [];
for (let i = 0; i < s.length; i++) {
rs[indices[i]] = s[i];
ans[indices[i]] = s[i];
}
return rs.join('');
return ans.join('');
};
```

Expand Down
4 changes: 2 additions & 2 deletions solution/1500-1599/1528.Shuffle String/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public String restoreString(String s, int[] indices) {
for (int i = 0; i < n; ++i) {
ans[indices[i]] = s.charAt(i);
}
return String.valueOf(ans);
return new String(ans);
}
}
}
6 changes: 3 additions & 3 deletions solution/1500-1599/1528.Shuffle String/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* @return {string}
*/
var restoreString = function (s, indices) {
let rs = [];
const ans = [];
for (let i = 0; i < s.length; i++) {
rs[indices[i]] = s[i];
ans[indices[i]] = s[i];
}
return rs.join('');
return ans.join('');
};
8 changes: 4 additions & 4 deletions solution/1500-1599/1528.Shuffle String/Solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution:
def restoreString(self, s: str, indices: List[int]) -> str:
ans = [0] * len(s)
for i, c in enumerate(s):
ans[indices[i]] = c
return ''.join(ans)
ans = [None] * len(s)
for c, j in zip(s, indices):
ans[j] = c
return "".join(ans)
11 changes: 11 additions & 0 deletions solution/1500-1599/1528.Shuffle String/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
impl Solution {
pub fn restore_string(s: String, indices: Vec<i32>) -> String {
let n = s.len();
let mut ans = vec![' '; n];
let chars: Vec<char> = s.chars().collect();
for i in 0..n {
ans[indices[i] as usize] = chars[i];
}
ans.iter().collect()
}
}
7 changes: 7 additions & 0 deletions solution/1500-1599/1528.Shuffle String/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function restoreString(s: string, indices: number[]): string {
const ans: string[] = [];
for (let i = 0; i < s.length; i++) {
ans[indices[i]] = s[i];
}
return ans.join('');
}