Skip to content

Commit b2f46cb

Browse files
authored
feat: add solutions to lc problem: No.3361 (#3808)
No.3361.Shift Distance Between Two Strings
1 parent b698cf8 commit b2f46cb

File tree

7 files changed

+320
-6
lines changed

7 files changed

+320
-6
lines changed

solution/3300-3399/3361.Shift Distance Between Two Strings/README.md

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,130 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3361.Sh
8787
#### Python3
8888

8989
```python
90-
90+
class Solution:
91+
def shiftDistance(
92+
self, s: str, t: str, nextCost: List[int], previousCost: List[int]
93+
) -> int:
94+
m = 26
95+
s1 = [0] * (m << 1 | 1)
96+
s2 = [0] * (m << 1 | 1)
97+
for i in range(m << 1):
98+
s1[i + 1] = s1[i] + nextCost[i % m]
99+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m]
100+
ans = 0
101+
for a, b in zip(s, t):
102+
x, y = ord(a) - ord("a"), ord(b) - ord("a")
103+
c1 = s1[y + m if y < x else y] - s1[x]
104+
c2 = s2[x + m if x < y else x] - s2[y]
105+
ans += min(c1, c2)
106+
return ans
91107
```
92108

93109
#### Java
94110

95111
```java
96-
112+
class Solution {
113+
public long shiftDistance(String s, String t, int[] nextCost, int[] previousCost) {
114+
int m = 26;
115+
long[] s1 = new long[(m << 1) + 1];
116+
long[] s2 = new long[(m << 1) + 1];
117+
for (int i = 0; i < (m << 1); i++) {
118+
s1[i + 1] = s1[i] + nextCost[i % m];
119+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m];
120+
}
121+
long ans = 0;
122+
for (int i = 0; i < s.length(); i++) {
123+
int x = s.charAt(i) - 'a';
124+
int y = t.charAt(i) - 'a';
125+
long c1 = s1[y + (y < x ? m : 0)] - s1[x];
126+
long c2 = s2[x + (x < y ? m : 0)] - s2[y];
127+
ans += Math.min(c1, c2);
128+
}
129+
return ans;
130+
}
131+
}
97132
```
98133

99134
#### C++
100135

101136
```cpp
102-
137+
class Solution {
138+
public:
139+
long long shiftDistance(string s, string t, vector<int>& nextCost, vector<int>& previousCost) {
140+
int m = 26;
141+
vector<long long> s1((m << 1) + 1);
142+
vector<long long> s2((m << 1) + 1);
143+
for (int i = 0; i < (m << 1); ++i) {
144+
s1[i + 1] = s1[i] + nextCost[i % m];
145+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m];
146+
}
147+
148+
long long ans = 0;
149+
for (int i = 0; i < s.size(); ++i) {
150+
int x = s[i] - 'a';
151+
int y = t[i] - 'a';
152+
long long c1 = s1[y + (y < x ? m : 0)] - s1[x];
153+
long long c2 = s2[x + (x < y ? m : 0)] - s2[y];
154+
ans += min(c1, c2);
155+
}
156+
157+
return ans;
158+
}
159+
};
103160
```
104161

105162
#### Go
106163

107164
```go
165+
func shiftDistance(s string, t string, nextCost []int, previousCost []int) (ans int64) {
166+
m := 26
167+
s1 := make([]int64, (m<<1)+1)
168+
s2 := make([]int64, (m<<1)+1)
169+
for i := 0; i < (m << 1); i++ {
170+
s1[i+1] = s1[i] + int64(nextCost[i%m])
171+
s2[i+1] = s2[i] + int64(previousCost[(i+1)%m])
172+
}
173+
for i := 0; i < len(s); i++ {
174+
x := int(s[i] - 'a')
175+
y := int(t[i] - 'a')
176+
z := y
177+
if y < x {
178+
z += m
179+
}
180+
c1 := s1[z] - s1[x]
181+
z = x
182+
if x < y {
183+
z += m
184+
}
185+
c2 := s2[z] - s2[y]
186+
ans += min(c1, c2)
187+
}
188+
return
189+
}
190+
```
108191

192+
#### TypeScript
193+
194+
```ts
195+
function shiftDistance(s: string, t: string, nextCost: number[], previousCost: number[]): number {
196+
const m = 26;
197+
const s1: number[] = Array((m << 1) + 1).fill(0);
198+
const s2: number[] = Array((m << 1) + 1).fill(0);
199+
for (let i = 0; i < m << 1; i++) {
200+
s1[i + 1] = s1[i] + nextCost[i % m];
201+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m];
202+
}
203+
let ans = 0;
204+
const a = 'a'.charCodeAt(0);
205+
for (let i = 0; i < s.length; i++) {
206+
const x = s.charCodeAt(i) - a;
207+
const y = t.charCodeAt(i) - a;
208+
const c1 = s1[y + (y < x ? m : 0)] - s1[x];
209+
const c2 = s2[x + (x < y ? m : 0)] - s2[y];
210+
ans += Math.min(c1, c2);
211+
}
212+
return ans;
213+
}
109214
```
110215

111216
<!-- tabs:end -->

solution/3300-3399/3361.Shift Distance Between Two Strings/README_EN.md

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,130 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3361.Sh
8585
#### Python3
8686

8787
```python
88-
88+
class Solution:
89+
def shiftDistance(
90+
self, s: str, t: str, nextCost: List[int], previousCost: List[int]
91+
) -> int:
92+
m = 26
93+
s1 = [0] * (m << 1 | 1)
94+
s2 = [0] * (m << 1 | 1)
95+
for i in range(m << 1):
96+
s1[i + 1] = s1[i] + nextCost[i % m]
97+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m]
98+
ans = 0
99+
for a, b in zip(s, t):
100+
x, y = ord(a) - ord("a"), ord(b) - ord("a")
101+
c1 = s1[y + m if y < x else y] - s1[x]
102+
c2 = s2[x + m if x < y else x] - s2[y]
103+
ans += min(c1, c2)
104+
return ans
89105
```
90106

91107
#### Java
92108

93109
```java
94-
110+
class Solution {
111+
public long shiftDistance(String s, String t, int[] nextCost, int[] previousCost) {
112+
int m = 26;
113+
long[] s1 = new long[(m << 1) + 1];
114+
long[] s2 = new long[(m << 1) + 1];
115+
for (int i = 0; i < (m << 1); i++) {
116+
s1[i + 1] = s1[i] + nextCost[i % m];
117+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m];
118+
}
119+
long ans = 0;
120+
for (int i = 0; i < s.length(); i++) {
121+
int x = s.charAt(i) - 'a';
122+
int y = t.charAt(i) - 'a';
123+
long c1 = s1[y + (y < x ? m : 0)] - s1[x];
124+
long c2 = s2[x + (x < y ? m : 0)] - s2[y];
125+
ans += Math.min(c1, c2);
126+
}
127+
return ans;
128+
}
129+
}
95130
```
96131

97132
#### C++
98133

99134
```cpp
100-
135+
class Solution {
136+
public:
137+
long long shiftDistance(string s, string t, vector<int>& nextCost, vector<int>& previousCost) {
138+
int m = 26;
139+
vector<long long> s1((m << 1) + 1);
140+
vector<long long> s2((m << 1) + 1);
141+
for (int i = 0; i < (m << 1); ++i) {
142+
s1[i + 1] = s1[i] + nextCost[i % m];
143+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m];
144+
}
145+
146+
long long ans = 0;
147+
for (int i = 0; i < s.size(); ++i) {
148+
int x = s[i] - 'a';
149+
int y = t[i] - 'a';
150+
long long c1 = s1[y + (y < x ? m : 0)] - s1[x];
151+
long long c2 = s2[x + (x < y ? m : 0)] - s2[y];
152+
ans += min(c1, c2);
153+
}
154+
155+
return ans;
156+
}
157+
};
101158
```
102159

103160
#### Go
104161

105162
```go
163+
func shiftDistance(s string, t string, nextCost []int, previousCost []int) (ans int64) {
164+
m := 26
165+
s1 := make([]int64, (m<<1)+1)
166+
s2 := make([]int64, (m<<1)+1)
167+
for i := 0; i < (m << 1); i++ {
168+
s1[i+1] = s1[i] + int64(nextCost[i%m])
169+
s2[i+1] = s2[i] + int64(previousCost[(i+1)%m])
170+
}
171+
for i := 0; i < len(s); i++ {
172+
x := int(s[i] - 'a')
173+
y := int(t[i] - 'a')
174+
z := y
175+
if y < x {
176+
z += m
177+
}
178+
c1 := s1[z] - s1[x]
179+
z = x
180+
if x < y {
181+
z += m
182+
}
183+
c2 := s2[z] - s2[y]
184+
ans += min(c1, c2)
185+
}
186+
return
187+
}
188+
```
106189

190+
#### TypeScript
191+
192+
```ts
193+
function shiftDistance(s: string, t: string, nextCost: number[], previousCost: number[]): number {
194+
const m = 26;
195+
const s1: number[] = Array((m << 1) + 1).fill(0);
196+
const s2: number[] = Array((m << 1) + 1).fill(0);
197+
for (let i = 0; i < m << 1; i++) {
198+
s1[i + 1] = s1[i] + nextCost[i % m];
199+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m];
200+
}
201+
let ans = 0;
202+
const a = 'a'.charCodeAt(0);
203+
for (let i = 0; i < s.length; i++) {
204+
const x = s.charCodeAt(i) - a;
205+
const y = t.charCodeAt(i) - a;
206+
const c1 = s1[y + (y < x ? m : 0)] - s1[x];
207+
const c2 = s2[x + (x < y ? m : 0)] - s2[y];
208+
ans += Math.min(c1, c2);
209+
}
210+
return ans;
211+
}
107212
```
108213

109214
<!-- tabs:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
long long shiftDistance(string s, string t, vector<int>& nextCost, vector<int>& previousCost) {
4+
int m = 26;
5+
vector<long long> s1((m << 1) + 1);
6+
vector<long long> s2((m << 1) + 1);
7+
for (int i = 0; i < (m << 1); ++i) {
8+
s1[i + 1] = s1[i] + nextCost[i % m];
9+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m];
10+
}
11+
12+
long long ans = 0;
13+
for (int i = 0; i < s.size(); ++i) {
14+
int x = s[i] - 'a';
15+
int y = t[i] - 'a';
16+
long long c1 = s1[y + (y < x ? m : 0)] - s1[x];
17+
long long c2 = s2[x + (x < y ? m : 0)] - s2[y];
18+
ans += min(c1, c2);
19+
}
20+
21+
return ans;
22+
}
23+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func shiftDistance(s string, t string, nextCost []int, previousCost []int) (ans int64) {
2+
m := 26
3+
s1 := make([]int64, (m<<1)+1)
4+
s2 := make([]int64, (m<<1)+1)
5+
for i := 0; i < (m << 1); i++ {
6+
s1[i+1] = s1[i] + int64(nextCost[i%m])
7+
s2[i+1] = s2[i] + int64(previousCost[(i+1)%m])
8+
}
9+
for i := 0; i < len(s); i++ {
10+
x := int(s[i] - 'a')
11+
y := int(t[i] - 'a')
12+
z := y
13+
if y < x {
14+
z += m
15+
}
16+
c1 := s1[z] - s1[x]
17+
z = x
18+
if x < y {
19+
z += m
20+
}
21+
c2 := s2[z] - s2[y]
22+
ans += min(c1, c2)
23+
}
24+
return
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public long shiftDistance(String s, String t, int[] nextCost, int[] previousCost) {
3+
int m = 26;
4+
long[] s1 = new long[(m << 1) + 1];
5+
long[] s2 = new long[(m << 1) + 1];
6+
for (int i = 0; i < (m << 1); i++) {
7+
s1[i + 1] = s1[i] + nextCost[i % m];
8+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m];
9+
}
10+
long ans = 0;
11+
for (int i = 0; i < s.length(); i++) {
12+
int x = s.charAt(i) - 'a';
13+
int y = t.charAt(i) - 'a';
14+
long c1 = s1[y + (y < x ? m : 0)] - s1[x];
15+
long c2 = s2[x + (x < y ? m : 0)] - s2[y];
16+
ans += Math.min(c1, c2);
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def shiftDistance(
3+
self, s: str, t: str, nextCost: List[int], previousCost: List[int]
4+
) -> int:
5+
m = 26
6+
s1 = [0] * (m << 1 | 1)
7+
s2 = [0] * (m << 1 | 1)
8+
for i in range(m << 1):
9+
s1[i + 1] = s1[i] + nextCost[i % m]
10+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m]
11+
ans = 0
12+
for a, b in zip(s, t):
13+
x, y = ord(a) - ord("a"), ord(b) - ord("a")
14+
c1 = s1[y + m if y < x else y] - s1[x]
15+
c2 = s2[x + m if x < y else x] - s2[y]
16+
ans += min(c1, c2)
17+
return ans
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function shiftDistance(s: string, t: string, nextCost: number[], previousCost: number[]): number {
2+
const m = 26;
3+
const s1: number[] = Array((m << 1) + 1).fill(0);
4+
const s2: number[] = Array((m << 1) + 1).fill(0);
5+
for (let i = 0; i < m << 1; i++) {
6+
s1[i + 1] = s1[i] + nextCost[i % m];
7+
s2[i + 1] = s2[i] + previousCost[(i + 1) % m];
8+
}
9+
let ans = 0;
10+
const a = 'a'.charCodeAt(0);
11+
for (let i = 0; i < s.length; i++) {
12+
const x = s.charCodeAt(i) - a;
13+
const y = t.charCodeAt(i) - a;
14+
const c1 = s1[y + (y < x ? m : 0)] - s1[x];
15+
const c2 = s2[x + (x < y ? m : 0)] - s2[y];
16+
ans += Math.min(c1, c2);
17+
}
18+
return ans;
19+
}

0 commit comments

Comments
 (0)