diff --git a/solution/2300-2399/2399.Check Distances Between Same Letters/README.md b/solution/2300-2399/2399.Check Distances Between Same Letters/README.md index 0b9c220b70ecd..49a83776c9d18 100644 --- a/solution/2300-2399/2399.Check Distances Between Same Letters/README.md +++ b/solution/2300-2399/2399.Check Distances Between Same Letters/README.md @@ -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$ 为字符集,这里为小写字母集合。 @@ -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 ``` @@ -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; @@ -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]) { diff --git a/solution/2300-2399/2399.Check Distances Between Same Letters/README_EN.md b/solution/2300-2399/2399.Check Distances Between Same Letters/README_EN.md index 3600ab493dfd1..7bae138a9253b 100644 --- a/solution/2300-2399/2399.Check Distances Between Same Letters/README_EN.md +++ b/solution/2300-2399/2399.Check Distances Between Same Letters/README_EN.md @@ -69,7 +69,11 @@ Because distance[0] = 1, s is not a well-spaced string. -### 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. @@ -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 ``` @@ -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; @@ -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]) { diff --git a/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.java b/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.java index 12c1bb4976aab..7c6c02bd5d9e1 100644 --- a/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.java +++ b/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.java @@ -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; @@ -10,4 +10,4 @@ public boolean checkDistances(String s, int[] distance) { } return true; } -} \ No newline at end of file +} diff --git a/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.py b/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.py index 3a18ef0d0095a..801f725071b60 100644 --- a/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.py +++ b/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.py @@ -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 diff --git a/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.ts b/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.ts index 3c13a0d6e8f4e..3d10b8da06a9f 100644 --- a/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.ts +++ b/solution/2300-2399/2399.Check Distances Between Same Letters/Solution.ts @@ -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]) { diff --git a/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README.md b/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README.md index 138b294bcc774..694502cbb95ac 100644 --- a/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README.md +++ b/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README.md @@ -134,7 +134,7 @@ public: const int mod = 1e9 + 7; int f[k + 1][k + 1]; memset(f, -1, sizeof(f)); - function dfs = [&](int i, int j) -> int { + auto dfs = [&](auto&& dfs, int i, int j) -> int { if (i > j || j < 0) { return 0; } @@ -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); } }; ``` diff --git a/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README_EN.md b/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README_EN.md index a3b5c0f5db4c0..e149e4a11f5fd 100644 --- a/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README_EN.md +++ b/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/README_EN.md @@ -135,7 +135,7 @@ public: const int mod = 1e9 + 7; int f[k + 1][k + 1]; memset(f, -1, sizeof(f)); - function dfs = [&](int i, int j) -> int { + auto dfs = [&](auto&& dfs, int i, int j) -> int { if (i > j || j < 0) { return 0; } @@ -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); } }; ``` diff --git a/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/Solution.cpp b/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/Solution.cpp index 3482f6ead1e61..ead496c17fd7e 100644 --- a/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/Solution.cpp +++ b/solution/2400-2499/2400.Number of Ways to Reach a Position After Exactly k Steps/Solution.cpp @@ -4,7 +4,7 @@ class Solution { const int mod = 1e9 + 7; int f[k + 1][k + 1]; memset(f, -1, sizeof(f)); - function dfs = [&](int i, int j) -> int { + auto dfs = [&](auto&& dfs, int i, int j) -> int { if (i > j || j < 0) { return 0; } @@ -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); } -}; \ No newline at end of file +};