Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.1427,1428 #2065

Merged
merged 3 commits into from
Dec 5, 2023
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
28 changes: 20 additions & 8 deletions solution/1400-1499/1427.Perform String Shifts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@

**方法一:模拟**

遍历 `shift`,累加(减)得到最终偏移量 $x$,取模后对字符串 `s` 进行左移或右移
我们不妨记字符串 $s$ 的长度为 $n$。接下来遍历数组 $shift$,累加得到最终的偏移量 $x$,然后将 $x$ 对 $n$ 取模,最终结果就是将 $s$ 的前 $n - x$ 个字符移动到末尾

时间复杂度 $O(n+m)$其中 $n$ 为字符串 `s` 的长度,$m$ 为 `shift` 的长度。
时间复杂度 $O(n + m)$其中 $n$ $m$ 分别是字符串 $s$ 的长度和数组 $shift$ 的长度。空间复杂度 $O(1)$

<!-- tabs:start -->

Expand All @@ -71,11 +71,7 @@
```python
class Solution:
def stringShift(self, s: str, shift: List[List[int]]) -> str:
x = 0
for a, b in shift:
if a == 0:
b = -b
x += b
x = sum((b if a else -b) for a, b in shift)
x %= len(s)
return s[-x:] + s[:-x]
```
Expand All @@ -90,7 +86,7 @@ class Solution {
int x = 0;
for (var e : shift) {
if (e[0] == 0) {
e[1] = -e[1];
e[1] *= -1;
}
x += e[1];
}
Expand Down Expand Up @@ -138,6 +134,22 @@ func stringShift(s string, shift [][]int) string {
}
```

### **TypeScript**

```ts
function stringShift(s: string, shift: number[][]): string {
let x = 0;
for (const [a, b] of shift) {
x += a === 0 ? -b : b;
}
x %= s.length;
if (x < 0) {
x += s.length;
}
return s.slice(-x) + s.slice(0, -x);
}
```

### **...**

```
Expand Down
30 changes: 24 additions & 6 deletions solution/1400-1499/1427.Perform String Shifts/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@

## Solutions

**Solution 1: Simulation**

We can denote the length of the string $s$ as $n$. Next, we traverse the array $shift$, accumulate to get the final offset $x$, then take $x$ modulo $n$, the final result is to move the first $n - x$ characters of $s$ to the end.

The time complexity is $O(n + m)$, where $n$ and $m$ are the lengths of the string $s$ and the array $shift$ respectively. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def stringShift(self, s: str, shift: List[List[int]]) -> str:
x = 0
for a, b in shift:
if a == 0:
b = -b
x += b
x = sum((b if a else -b) for a, b in shift)
x %= len(s)
return s[-x:] + s[:-x]
```
Expand All @@ -74,7 +76,7 @@ class Solution {
int x = 0;
for (var e : shift) {
if (e[0] == 0) {
e[1] = -e[1];
e[1] *= -1;
}
x += e[1];
}
Expand Down Expand Up @@ -122,6 +124,22 @@ func stringShift(s string, shift [][]int) string {
}
```

### **TypeScript**

```ts
function stringShift(s: string, shift: number[][]): string {
let x = 0;
for (const [a, b] of shift) {
x += a === 0 ? -b : b;
}
x %= s.length;
if (x < 0) {
x += s.length;
}
return s.slice(-x) + s.slice(0, -x);
}
```

### **...**

```
Expand Down
26 changes: 13 additions & 13 deletions solution/1400-1499/1427.Perform String Shifts/Solution.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class Solution {
public String stringShift(String s, int[][] shift) {
int x = 0;
for (var e : shift) {
if (e[0] == 0) {
e[1] = -e[1];
}
x += e[1];
}
int n = s.length();
x = (x % n + n) % n;
return s.substring(n - x) + s.substring(0, n - x);
}
class Solution {
public String stringShift(String s, int[][] shift) {
int x = 0;
for (var e : shift) {
if (e[0] == 0) {
e[1] *= -1;
}
x += e[1];
}
int n = s.length();
x = (x % n + n) % n;
return s.substring(n - x) + s.substring(0, n - x);
}
}
14 changes: 5 additions & 9 deletions solution/1400-1499/1427.Perform String Shifts/Solution.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
class Solution:
def stringShift(self, s: str, shift: List[List[int]]) -> str:
x = 0
for a, b in shift:
if a == 0:
b = -b
x += b
x %= len(s)
return s[-x:] + s[:-x]
class Solution:
def stringShift(self, s: str, shift: List[List[int]]) -> str:
x = sum((b if a else -b) for a, b in shift)
x %= len(s)
return s[-x:] + s[:-x]
11 changes: 11 additions & 0 deletions solution/1400-1499/1427.Perform String Shifts/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function stringShift(s: string, shift: number[][]): string {
let x = 0;
for (const [a, b] of shift) {
x += a === 0 ? -b : b;
}
x %= s.length;
if (x < 0) {
x += s.length;
}
return s.slice(-x) + s.slice(0, -x);
}
Loading