Skip to content

Commit 51b2191

Browse files
committed
feat: add solutions to lc problem: No.0848
No.0848.Shifting Letters
1 parent 37deef7 commit 51b2191

File tree

6 files changed

+201
-12
lines changed

6 files changed

+201
-12
lines changed

solution/0800-0899/0848.Shifting Letters/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,103 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:后缀和**
59+
60+
对于字符串 $s$ 中的每个字符,我们需要计算其最终的偏移量,即 `shifts[i]``shifts[i + 1]``shifts[i + 2]` ... 的和。我们可以使用后缀和的思想,从后往前遍历 `shifts`,计算每个字符的最终偏移量,然后对 $26$ 取模,得到最终的字符。
61+
62+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
63+
5864
<!-- tabs:start -->
5965

6066
### **Python3**
6167

6268
<!-- 这里可写当前语言的特殊实现逻辑 -->
6369

6470
```python
71+
class Solution:
72+
def shiftingLetters(self, s: str, shifts: List[int]) -> str:
73+
n = len(s)
74+
d = [0] * (n + 1)
75+
for i, c in enumerate(s):
76+
v = ord(c) - ord('a')
77+
d[i] += v
78+
d[i + 1] -= v
79+
for i, x in enumerate(shifts):
80+
d[0] += x
81+
d[i + 1] -= x
82+
t = 0
83+
ans = []
84+
for i in range(n):
85+
d[i] %= 26
86+
ans.append(ascii_lowercase[d[i]])
87+
d[i + 1] += d[i]
88+
return ''.join(ans)
89+
```
6590

91+
```python
92+
class Solution:
93+
def shiftingLetters(self, s: str, shifts: List[int]) -> str:
94+
n, t = len(s), 0
95+
s = list(s)
96+
for i in range(n - 1, -1, -1):
97+
t += shifts[i]
98+
j = (ord(s[i]) - ord('a') + t) % 26
99+
s[i] = ascii_lowercase[j]
100+
return ''.join(s)
66101
```
67102

68103
### **Java**
69104

70105
<!-- 这里可写当前语言的特殊实现逻辑 -->
71106

72107
```java
108+
class Solution {
109+
public String shiftingLetters(String s, int[] shifts) {
110+
char[] cs = s.toCharArray();
111+
int n = cs.length;
112+
long t = 0;
113+
for (int i = n - 1; i >= 0; --i) {
114+
t += shifts[i];
115+
int j = (int) ((cs[i] - 'a' + t) % 26);
116+
cs[i] = (char) ('a' + j);
117+
}
118+
return String.valueOf(cs);
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
string shiftingLetters(string s, vector<int>& shifts) {
129+
long long t = 0;
130+
int n = s.size();
131+
for (int i = n - 1; ~i; --i) {
132+
t += shifts[i];
133+
int j = (s[i] - 'a' + t) % 26;
134+
s[i] = 'a' + j;
135+
}
136+
return s;
137+
}
138+
};
139+
```
73140
141+
### **Go**
142+
143+
```go
144+
func shiftingLetters(s string, shifts []int) string {
145+
t := 0
146+
n := len(s)
147+
cs := []byte(s)
148+
for i := n - 1; i >= 0; i-- {
149+
t += shifts[i]
150+
j := (int(cs[i]-'a') + t) % 26
151+
cs[i] = byte('a' + j)
152+
}
153+
return string(cs)
154+
}
74155
```
75156

76157
### **...**

solution/0800-0899/0848.Shifting Letters/README_EN.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,88 @@ After shifting the first 3 letters of s by 9, we have &quot;rpl&quot;, the answe
5252
### **Python3**
5353

5454
```python
55+
class Solution:
56+
def shiftingLetters(self, s: str, shifts: List[int]) -> str:
57+
n = len(s)
58+
d = [0] * (n + 1)
59+
for i, c in enumerate(s):
60+
v = ord(c) - ord('a')
61+
d[i] += v
62+
d[i + 1] -= v
63+
for i, x in enumerate(shifts):
64+
d[0] += x
65+
d[i + 1] -= x
66+
t = 0
67+
ans = []
68+
for i in range(n):
69+
d[i] %= 26
70+
ans.append(ascii_lowercase[d[i]])
71+
d[i + 1] += d[i]
72+
return ''.join(ans)
73+
```
5574

75+
```python
76+
class Solution:
77+
def shiftingLetters(self, s: str, shifts: List[int]) -> str:
78+
n, t = len(s), 0
79+
s = list(s)
80+
for i in range(n - 1, -1, -1):
81+
t += shifts[i]
82+
j = (ord(s[i]) - ord('a') + t) % 26
83+
s[i] = ascii_lowercase[j]
84+
return ''.join(s)
5685
```
5786

5887
### **Java**
5988

6089
```java
90+
class Solution {
91+
public String shiftingLetters(String s, int[] shifts) {
92+
char[] cs = s.toCharArray();
93+
int n = cs.length;
94+
long t = 0;
95+
for (int i = n - 1; i >= 0; --i) {
96+
t += shifts[i];
97+
int j = (int) ((cs[i] - 'a' + t) % 26);
98+
cs[i] = (char) ('a' + j);
99+
}
100+
return String.valueOf(cs);
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
string shiftingLetters(string s, vector<int>& shifts) {
111+
long long t = 0;
112+
int n = s.size();
113+
for (int i = n - 1; ~i; --i) {
114+
t += shifts[i];
115+
int j = (s[i] - 'a' + t) % 26;
116+
s[i] = 'a' + j;
117+
}
118+
return s;
119+
}
120+
};
121+
```
61122
123+
### **Go**
124+
125+
```go
126+
func shiftingLetters(s string, shifts []int) string {
127+
t := 0
128+
n := len(s)
129+
cs := []byte(s)
130+
for i := n - 1; i >= 0; i-- {
131+
t += shifts[i]
132+
j := (int(cs[i]-'a') + t) % 26
133+
cs[i] = byte('a' + j)
134+
}
135+
return string(cs)
136+
}
62137
```
63138

64139
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
string shiftingLetters(string s, vector<int>& shifts) {
4+
long long t = 0;
5+
int n = s.size();
6+
for (int i = n - 1; ~i; --i) {
7+
t += shifts[i];
8+
int j = (s[i] - 'a' + t) % 26;
9+
s[i] = 'a' + j;
10+
}
11+
return s;
12+
}
13+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func shiftingLetters(s string, shifts []int) string {
2+
t := 0
3+
n := len(s)
4+
cs := []byte(s)
5+
for i := n - 1; i >= 0; i-- {
6+
t += shifts[i]
7+
j := (int(cs[i]-'a') + t) % 26
8+
cs[i] = byte('a' + j)
9+
}
10+
return string(cs)
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public String shiftingLetters(String s, int[] shifts) {
3+
char[] cs = s.toCharArray();
4+
int n = cs.length;
5+
long t = 0;
6+
for (int i = n - 1; i >= 0; --i) {
7+
t += shifts[i];
8+
int j = (int) ((cs[i] - 'a' + t) % 26);
9+
cs[i] = (char) ('a' + j);
10+
}
11+
return String.valueOf(cs);
12+
}
13+
}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
class Solution:
2-
def shiftingLetters(self, S, shifts):
3-
"""
4-
:type S: str
5-
:type shifts: List[int]
6-
:rtype: str
7-
"""
8-
mov = 0
9-
ans = list(S)
10-
for i in range(len(S) - 1, -1, -1):
11-
mov += shifts[i]
12-
ans[i] = chr((ord(S[i]) - 97 + mov % 26) % 26 + 97)
13-
return ''.join(ans)
2+
def shiftingLetters(self, s: str, shifts: List[int]) -> str:
3+
n, t = len(s), 0
4+
s = list(s)
5+
for i in range(n - 1, -1, -1):
6+
t += shifts[i]
7+
j = (ord(s[i]) - ord('a') + t) % 26
8+
s[i] = ascii_lowercase[j]
9+
return ''.join(s)

0 commit comments

Comments
 (0)