Skip to content

feat: update solutions to lc problems: No.2399,2400 #3519

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

Merged
merged 1 commit into from
Sep 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ tags:

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

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

<!-- tabs:start -->

Expand All @@ -83,10 +83,11 @@ tags:
class Solution:
def checkDistances(self, s: str, distance: List[int]) -> bool:
d = defaultdict(int)
for i, c in enumerate(s, 1):
if d[c] and i - d[c] - 1 != distance[ord(c) - ord('a')]:
for i, c in enumerate(map(ord, s), 1):
j = c - ord("a")
if d[j] and i - d[j] - 1 != distance[j]:
return False
d[c] = i
d[j] = i
return True
```

Expand All @@ -96,7 +97,7 @@ class Solution:
class Solution {
public boolean checkDistances(String s, int[] distance) {
int[] d = new int[26];
for (int i = 1, n = s.length(); i <= n; ++i) {
for (int i = 1; i <= s.length(); ++i) {
int j = s.charAt(i - 1) - 'a';
if (d[j] > 0 && i - d[j] - 1 != distance[j]) {
return false;
Expand Down Expand Up @@ -147,8 +148,8 @@ func checkDistances(s string, distance []int) bool {

```ts
function checkDistances(s: string, distance: number[]): boolean {
const d: number[] = Array(26).fill(0);
const n = s.length;
const d: number[] = new Array(26).fill(0);
for (let i = 1; i <= n; ++i) {
const j = s.charCodeAt(i - 1) - 97;
if (d[j] && i - d[j] - 1 !== distance[j]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ Because distance[0] = 1, s is not a well-spaced string.

<!-- solution:start -->

### Solution 1
### Solution 1: Array or Hash Table

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`.

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.

<!-- tabs:start -->

Expand All @@ -79,10 +83,11 @@ Because distance[0] = 1, s is not a well-spaced string.
class Solution:
def checkDistances(self, s: str, distance: List[int]) -> bool:
d = defaultdict(int)
for i, c in enumerate(s, 1):
if d[c] and i - d[c] - 1 != distance[ord(c) - ord('a')]:
for i, c in enumerate(map(ord, s), 1):
j = c - ord("a")
if d[j] and i - d[j] - 1 != distance[j]:
return False
d[c] = i
d[j] = i
return True
```

Expand All @@ -92,7 +97,7 @@ class Solution:
class Solution {
public boolean checkDistances(String s, int[] distance) {
int[] d = new int[26];
for (int i = 1, n = s.length(); i <= n; ++i) {
for (int i = 1; i <= s.length(); ++i) {
int j = s.charAt(i - 1) - 'a';
if (d[j] > 0 && i - d[j] - 1 != distance[j]) {
return false;
Expand Down Expand Up @@ -143,8 +148,8 @@ func checkDistances(s string, distance []int) bool {

```ts
function checkDistances(s: string, distance: number[]): boolean {
const d: number[] = Array(26).fill(0);
const n = s.length;
const d: number[] = new Array(26).fill(0);
for (let i = 1; i <= n; ++i) {
const j = s.charCodeAt(i - 1) - 97;
if (d[j] && i - d[j] - 1 !== distance[j]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution {
public boolean checkDistances(String s, int[] distance) {
int[] d = new int[26];
for (int i = 1, n = s.length(); i <= n; ++i) {
for (int i = 1; i <= s.length(); ++i) {
int j = s.charAt(i - 1) - 'a';
if (d[j] > 0 && i - d[j] - 1 != distance[j]) {
return false;
Expand All @@ -10,4 +10,4 @@ public boolean checkDistances(String s, int[] distance) {
}
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Solution:
def checkDistances(self, s: str, distance: List[int]) -> bool:
d = defaultdict(int)
for i, c in enumerate(s, 1):
if d[c] and i - d[c] - 1 != distance[ord(c) - ord('a')]:
for i, c in enumerate(map(ord, s), 1):
j = c - ord("a")
if d[j] and i - d[j] - 1 != distance[j]:
return False
d[c] = i
d[j] = i
return True
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function checkDistances(s: string, distance: number[]): boolean {
const d: number[] = Array(26).fill(0);
const n = s.length;
const d: number[] = new Array(26).fill(0);
for (let i = 1; i <= n; ++i) {
const j = s.charCodeAt(i - 1) - 97;
if (d[j] && i - d[j] - 1 !== distance[j]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public:
const int mod = 1e9 + 7;
int f[k + 1][k + 1];
memset(f, -1, sizeof(f));
function<int(int, int)> dfs = [&](int i, int j) -> int {
auto dfs = [&](auto&& dfs, int i, int j) -> int {
if (i > j || j < 0) {
return 0;
}
Expand All @@ -144,10 +144,10 @@ public:
if (f[i][j] != -1) {
return f[i][j];
}
f[i][j] = (dfs(i + 1, j - 1) + dfs(abs(i - 1), j - 1)) % mod;
f[i][j] = (dfs(dfs, i + 1, j - 1) + dfs(dfs, abs(i - 1), j - 1)) % mod;
return f[i][j];
};
return dfs(abs(startPos - endPos), k);
return dfs(dfs, abs(startPos - endPos), k);
}
};
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public:
const int mod = 1e9 + 7;
int f[k + 1][k + 1];
memset(f, -1, sizeof(f));
function<int(int, int)> dfs = [&](int i, int j) -> int {
auto dfs = [&](auto&& dfs, int i, int j) -> int {
if (i > j || j < 0) {
return 0;
}
Expand All @@ -145,10 +145,10 @@ public:
if (f[i][j] != -1) {
return f[i][j];
}
f[i][j] = (dfs(i + 1, j - 1) + dfs(abs(i - 1), j - 1)) % mod;
f[i][j] = (dfs(dfs, i + 1, j - 1) + dfs(dfs, abs(i - 1), j - 1)) % mod;
return f[i][j];
};
return dfs(abs(startPos - endPos), k);
return dfs(dfs, abs(startPos - endPos), k);
}
};
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Solution {
const int mod = 1e9 + 7;
int f[k + 1][k + 1];
memset(f, -1, sizeof(f));
function<int(int, int)> dfs = [&](int i, int j) -> int {
auto dfs = [&](auto&& dfs, int i, int j) -> int {
if (i > j || j < 0) {
return 0;
}
Expand All @@ -14,9 +14,9 @@ class Solution {
if (f[i][j] != -1) {
return f[i][j];
}
f[i][j] = (dfs(i + 1, j - 1) + dfs(abs(i - 1), j - 1)) % mod;
f[i][j] = (dfs(dfs, i + 1, j - 1) + dfs(dfs, abs(i - 1), j - 1)) % mod;
return f[i][j];
};
return dfs(abs(startPos - endPos), k);
return dfs(dfs, abs(startPos - endPos), k);
}
};
};
Loading