Skip to content

Commit 599ad45

Browse files
authored
feat: update solutions to lc problems: No.2399,2400 (doocs#3519)
1 parent a7de3e5 commit 599ad45

File tree

8 files changed

+35
-28
lines changed

8 files changed

+35
-28
lines changed

solution/2300-2399/2399.Check Distances Between Same Letters/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ tags:
7373

7474
我们可以用哈希表 $d$ 记录每个字母出现的下标,然后遍历哈希表,判断每个字母的下标之差是否等于 `distance` 中对应的值。如果出现不等的情况,直接返回 `false`。如果遍历结束后,没有出现不等的情况,返回 `true`
7575

76-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字符集大小,本题中 $C = 26$
76+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 为字符集,这里为小写字母集合
7777

7878
<!-- tabs:start -->
7979

@@ -83,10 +83,11 @@ tags:
8383
class Solution:
8484
def checkDistances(self, s: str, distance: List[int]) -> bool:
8585
d = defaultdict(int)
86-
for i, c in enumerate(s, 1):
87-
if d[c] and i - d[c] - 1 != distance[ord(c) - ord('a')]:
86+
for i, c in enumerate(map(ord, s), 1):
87+
j = c - ord("a")
88+
if d[j] and i - d[j] - 1 != distance[j]:
8889
return False
89-
d[c] = i
90+
d[j] = i
9091
return True
9192
```
9293

@@ -96,7 +97,7 @@ class Solution:
9697
class Solution {
9798
public boolean checkDistances(String s, int[] distance) {
9899
int[] d = new int[26];
99-
for (int i = 1, n = s.length(); i <= n; ++i) {
100+
for (int i = 1; i <= s.length(); ++i) {
100101
int j = s.charAt(i - 1) - 'a';
101102
if (d[j] > 0 && i - d[j] - 1 != distance[j]) {
102103
return false;
@@ -147,8 +148,8 @@ func checkDistances(s string, distance []int) bool {
147148

148149
```ts
149150
function checkDistances(s: string, distance: number[]): boolean {
151+
const d: number[] = Array(26).fill(0);
150152
const n = s.length;
151-
const d: number[] = new Array(26).fill(0);
152153
for (let i = 1; i <= n; ++i) {
153154
const j = s.charCodeAt(i - 1) - 97;
154155
if (d[j] && i - d[j] - 1 !== distance[j]) {

solution/2300-2399/2399.Check Distances Between Same Letters/README_EN.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ Because distance[0] = 1, s is not a well-spaced string.
6969

7070
<!-- solution:start -->
7171

72-
### Solution 1
72+
### Solution 1: Array or Hash Table
73+
74+
We can use a hash table $d$ to record the indices of each letter's occurrences. Then, traverse the hash table and check if the difference between the indices of each letter equals the corresponding value in the `distance` array. If any discrepancy is found, return `false`. If the traversal completes without discrepancies, return `true`.
75+
76+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set, which in this case is the set of lowercase letters.
7377

7478
<!-- tabs:start -->
7579

@@ -79,10 +83,11 @@ Because distance[0] = 1, s is not a well-spaced string.
7983
class Solution:
8084
def checkDistances(self, s: str, distance: List[int]) -> bool:
8185
d = defaultdict(int)
82-
for i, c in enumerate(s, 1):
83-
if d[c] and i - d[c] - 1 != distance[ord(c) - ord('a')]:
86+
for i, c in enumerate(map(ord, s), 1):
87+
j = c - ord("a")
88+
if d[j] and i - d[j] - 1 != distance[j]:
8489
return False
85-
d[c] = i
90+
d[j] = i
8691
return True
8792
```
8893

@@ -92,7 +97,7 @@ class Solution:
9297
class Solution {
9398
public boolean checkDistances(String s, int[] distance) {
9499
int[] d = new int[26];
95-
for (int i = 1, n = s.length(); i <= n; ++i) {
100+
for (int i = 1; i <= s.length(); ++i) {
96101
int j = s.charAt(i - 1) - 'a';
97102
if (d[j] > 0 && i - d[j] - 1 != distance[j]) {
98103
return false;
@@ -143,8 +148,8 @@ func checkDistances(s string, distance []int) bool {
143148

144149
```ts
145150
function checkDistances(s: string, distance: number[]): boolean {
151+
const d: number[] = Array(26).fill(0);
146152
const n = s.length;
147-
const d: number[] = new Array(26).fill(0);
148153
for (let i = 1; i <= n; ++i) {
149154
const j = s.charCodeAt(i - 1) - 97;
150155
if (d[j] && i - d[j] - 1 !== distance[j]) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution {
22
public boolean checkDistances(String s, int[] distance) {
33
int[] d = new int[26];
4-
for (int i = 1, n = s.length(); i <= n; ++i) {
4+
for (int i = 1; i <= s.length(); ++i) {
55
int j = s.charAt(i - 1) - 'a';
66
if (d[j] > 0 && i - d[j] - 1 != distance[j]) {
77
return false;
@@ -10,4 +10,4 @@ public boolean checkDistances(String s, int[] distance) {
1010
}
1111
return true;
1212
}
13-
}
13+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
class Solution:
22
def checkDistances(self, s: str, distance: List[int]) -> bool:
33
d = defaultdict(int)
4-
for i, c in enumerate(s, 1):
5-
if d[c] and i - d[c] - 1 != distance[ord(c) - ord('a')]:
4+
for i, c in enumerate(map(ord, s), 1):
5+
j = c - ord("a")
6+
if d[j] and i - d[j] - 1 != distance[j]:
67
return False
7-
d[c] = i
8+
d[j] = i
89
return True

solution/2300-2399/2399.Check Distances Between Same Letters/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function checkDistances(s: string, distance: number[]): boolean {
2+
const d: number[] = Array(26).fill(0);
23
const n = s.length;
3-
const d: number[] = new Array(26).fill(0);
44
for (let i = 1; i <= n; ++i) {
55
const j = s.charCodeAt(i - 1) - 97;
66
if (d[j] && i - d[j] - 1 !== distance[j]) {

solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public:
134134
const int mod = 1e9 + 7;
135135
int f[k + 1][k + 1];
136136
memset(f, -1, sizeof(f));
137-
function<int(int, int)> dfs = [&](int i, int j) -> int {
137+
auto dfs = [&](auto&& dfs, int i, int j) -> int {
138138
if (i > j || j < 0) {
139139
return 0;
140140
}
@@ -144,10 +144,10 @@ public:
144144
if (f[i][j] != -1) {
145145
return f[i][j];
146146
}
147-
f[i][j] = (dfs(i + 1, j - 1) + dfs(abs(i - 1), j - 1)) % mod;
147+
f[i][j] = (dfs(dfs, i + 1, j - 1) + dfs(dfs, abs(i - 1), j - 1)) % mod;
148148
return f[i][j];
149149
};
150-
return dfs(abs(startPos - endPos), k);
150+
return dfs(dfs, abs(startPos - endPos), k);
151151
}
152152
};
153153
```

solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public:
135135
const int mod = 1e9 + 7;
136136
int f[k + 1][k + 1];
137137
memset(f, -1, sizeof(f));
138-
function<int(int, int)> dfs = [&](int i, int j) -> int {
138+
auto dfs = [&](auto&& dfs, int i, int j) -> int {
139139
if (i > j || j < 0) {
140140
return 0;
141141
}
@@ -145,10 +145,10 @@ public:
145145
if (f[i][j] != -1) {
146146
return f[i][j];
147147
}
148-
f[i][j] = (dfs(i + 1, j - 1) + dfs(abs(i - 1), j - 1)) % mod;
148+
f[i][j] = (dfs(dfs, i + 1, j - 1) + dfs(dfs, abs(i - 1), j - 1)) % mod;
149149
return f[i][j];
150150
};
151-
return dfs(abs(startPos - endPos), k);
151+
return dfs(dfs, abs(startPos - endPos), k);
152152
}
153153
};
154154
```

solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/Solution.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Solution {
44
const int mod = 1e9 + 7;
55
int f[k + 1][k + 1];
66
memset(f, -1, sizeof(f));
7-
function<int(int, int)> dfs = [&](int i, int j) -> int {
7+
auto dfs = [&](auto&& dfs, int i, int j) -> int {
88
if (i > j || j < 0) {
99
return 0;
1010
}
@@ -14,9 +14,9 @@ class Solution {
1414
if (f[i][j] != -1) {
1515
return f[i][j];
1616
}
17-
f[i][j] = (dfs(i + 1, j - 1) + dfs(abs(i - 1), j - 1)) % mod;
17+
f[i][j] = (dfs(dfs, i + 1, j - 1) + dfs(dfs, abs(i - 1), j - 1)) % mod;
1818
return f[i][j];
1919
};
20-
return dfs(abs(startPos - endPos), k);
20+
return dfs(dfs, abs(startPos - endPos), k);
2121
}
22-
};
22+
};

0 commit comments

Comments
 (0)