Skip to content

Commit c89e3b7

Browse files
committed
feat: update solutions to lc problem: No.2399
No.2399.Check Distances Between Same Letters
1 parent 8f5467c commit c89e3b7

File tree

7 files changed

+68
-71
lines changed

7 files changed

+68
-71
lines changed

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

+24-25
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555

5656
**方法一:数组或哈希表**
5757

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

60-
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。
60+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字符集大小,本题中 $C = 26$
6161

6262
<!-- tabs:start -->
6363

@@ -68,12 +68,11 @@
6868
```python
6969
class Solution:
7070
def checkDistances(self, s: str, distance: List[int]) -> bool:
71-
d = [0] * 26
72-
for i, c in enumerate(s):
73-
j = ord(c) - ord("a")
74-
if d[j] and i - d[j] != distance[j]:
71+
d = defaultdict(int)
72+
for i, c in enumerate(s, 1):
73+
if d[c] and i - d[c] - 1 != distance[ord(c) - ord('a')]:
7574
return False
76-
d[j] = i + 1
75+
d[c] = i
7776
return True
7877
```
7978

@@ -85,12 +84,12 @@ class Solution:
8584
class Solution {
8685
public boolean checkDistances(String s, int[] distance) {
8786
int[] d = new int[26];
88-
for (int i = 0; i < s.length(); ++i) {
89-
int j = s.charAt(i) - 'a';
90-
if (d[j] > 0 && i - d[j] != distance[j]) {
87+
for (int i = 1, n = s.length(); i <= n; ++i) {
88+
int j = s.charAt(i - 1) - 'a';
89+
if (d[j] > 0 && i - d[j] - 1 != distance[j]) {
9190
return false;
9291
}
93-
d[j] = i + 1;
92+
d[j] = i;
9493
}
9594
return true;
9695
}
@@ -103,13 +102,13 @@ class Solution {
103102
class Solution {
104103
public:
105104
bool checkDistances(string s, vector<int>& distance) {
106-
vector<int> d(26);
107-
for (int i = 0; i < s.size(); ++i) {
108-
int j = s[i] - 'a';
109-
if (d[j] && i - d[j] != distance[j]) {
105+
int d[26]{};
106+
for (int i = 1; i <= s.size(); ++i) {
107+
int j = s[i - 1] - 'a';
108+
if (d[j] && i - d[j] - 1 != distance[j]) {
110109
return false;
111110
}
112-
d[j] = i + 1;
111+
d[j] = i;
113112
}
114113
return true;
115114
}
@@ -120,13 +119,13 @@ public:
120119
121120
```go
122121
func checkDistances(s string, distance []int) bool {
123-
d := make([]int, 26)
122+
d := [26]int{}
124123
for i, c := range s {
125-
j := c - 'a'
126-
if d[j] > 0 && i-d[j] != distance[j] {
124+
c -= 'a'
125+
if d[c] > 0 && i-d[c] != distance[c] {
127126
return false
128127
}
129-
d[j] = i + 1
128+
d[c] = i + 1
130129
}
131130
return true
132131
}
@@ -154,13 +153,13 @@ bool checkDistances(char *s, int *distance, int distanceSize) {
154153
```ts
155154
function checkDistances(s: string, distance: number[]): boolean {
156155
const n = s.length;
157-
const d = new Array(26).fill(0);
158-
for (let i = 0; i < n; i++) {
159-
const j = s[i].charCodeAt(0) - 'a'.charCodeAt(0);
160-
if (d[j] > 0 && i - d[j] !== distance[j]) {
156+
const d: number[] = new Array(26).fill(0);
157+
for (let i = 1; i <= n; ++i) {
158+
const j = s.charCodeAt(i - 1) - 97;
159+
if (d[j] && i - d[j] - 1 != distance[j]) {
161160
return false;
162161
}
163-
d[j] = i + 1;
162+
d[j] = i;
164163
}
165164
return true;
166165
}

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

+22-23
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,11 @@ Because distance[0] = 1, s is not a well-spaced string.
5656
```python
5757
class Solution:
5858
def checkDistances(self, s: str, distance: List[int]) -> bool:
59-
d = [0] * 26
60-
for i, c in enumerate(s):
61-
j = ord(c) - ord("a")
62-
if d[j] and i - d[j] != distance[j]:
59+
d = defaultdict(int)
60+
for i, c in enumerate(s, 1):
61+
if d[c] and i - d[c] - 1 != distance[ord(c) - ord('a')]:
6362
return False
64-
d[j] = i + 1
63+
d[c] = i
6564
return True
6665
```
6766

@@ -71,12 +70,12 @@ class Solution:
7170
class Solution {
7271
public boolean checkDistances(String s, int[] distance) {
7372
int[] d = new int[26];
74-
for (int i = 0; i < s.length(); ++i) {
75-
int j = s.charAt(i) - 'a';
76-
if (d[j] > 0 && i - d[j] != distance[j]) {
73+
for (int i = 1, n = s.length(); i <= n; ++i) {
74+
int j = s.charAt(i - 1) - 'a';
75+
if (d[j] > 0 && i - d[j] - 1 != distance[j]) {
7776
return false;
7877
}
79-
d[j] = i + 1;
78+
d[j] = i;
8079
}
8180
return true;
8281
}
@@ -89,13 +88,13 @@ class Solution {
8988
class Solution {
9089
public:
9190
bool checkDistances(string s, vector<int>& distance) {
92-
vector<int> d(26);
93-
for (int i = 0; i < s.size(); ++i) {
94-
int j = s[i] - 'a';
95-
if (d[j] && i - d[j] != distance[j]) {
91+
int d[26]{};
92+
for (int i = 1; i <= s.size(); ++i) {
93+
int j = s[i - 1] - 'a';
94+
if (d[j] && i - d[j] - 1 != distance[j]) {
9695
return false;
9796
}
98-
d[j] = i + 1;
97+
d[j] = i;
9998
}
10099
return true;
101100
}
@@ -106,13 +105,13 @@ public:
106105
107106
```go
108107
func checkDistances(s string, distance []int) bool {
109-
d := make([]int, 26)
108+
d := [26]int{}
110109
for i, c := range s {
111-
j := c - 'a'
112-
if d[j] > 0 && i-d[j] != distance[j] {
110+
c -= 'a'
111+
if d[c] > 0 && i-d[c] != distance[c] {
113112
return false
114113
}
115-
d[j] = i + 1
114+
d[c] = i + 1
116115
}
117116
return true
118117
}
@@ -140,13 +139,13 @@ bool checkDistances(char *s, int *distance, int distanceSize) {
140139
```ts
141140
function checkDistances(s: string, distance: number[]): boolean {
142141
const n = s.length;
143-
const d = new Array(26).fill(0);
144-
for (let i = 0; i < n; i++) {
145-
const j = s[i].charCodeAt(0) - 'a'.charCodeAt(0);
146-
if (d[j] > 0 && i - d[j] !== distance[j]) {
142+
const d: number[] = new Array(26).fill(0);
143+
for (let i = 1; i <= n; ++i) {
144+
const j = s.charCodeAt(i - 1) - 97;
145+
if (d[j] && i - d[j] - 1 != distance[j]) {
147146
return false;
148147
}
149-
d[j] = i + 1;
148+
d[j] = i;
150149
}
151150
return true;
152151
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public:
33
bool checkDistances(string s, vector<int>& distance) {
4-
vector<int> d(26);
5-
for (int i = 0; i < s.size(); ++i) {
6-
int j = s[i] - 'a';
7-
if (d[j] && i - d[j] != distance[j]) {
4+
int d[26]{};
5+
for (int i = 1; i <= s.size(); ++i) {
6+
int j = s[i - 1] - 'a';
7+
if (d[j] && i - d[j] - 1 != distance[j]) {
88
return false;
99
}
10-
d[j] = i + 1;
10+
d[j] = i;
1111
}
1212
return true;
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
func checkDistances(s string, distance []int) bool {
2-
d := make([]int, 26)
2+
d := [26]int{}
33
for i, c := range s {
4-
j := c - 'a'
5-
if d[j] > 0 && i-d[j] != distance[j] {
4+
c -= 'a'
5+
if d[c] > 0 && i-d[c] != distance[c] {
66
return false
77
}
8-
d[j] = i + 1
8+
d[c] = i + 1
99
}
1010
return true
1111
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public boolean checkDistances(String s, int[] distance) {
33
int[] d = new int[26];
4-
for (int i = 0; i < s.length(); ++i) {
5-
int j = s.charAt(i) - 'a';
6-
if (d[j] > 0 && i - d[j] != distance[j]) {
4+
for (int i = 1, n = s.length(); i <= n; ++i) {
5+
int j = s.charAt(i - 1) - 'a';
6+
if (d[j] > 0 && i - d[j] - 1 != distance[j]) {
77
return false;
88
}
9-
d[j] = i + 1;
9+
d[j] = i;
1010
}
1111
return true;
1212
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
class Solution:
22
def checkDistances(self, s: str, distance: List[int]) -> bool:
3-
d = [0] * 26
4-
for i, c in enumerate(s):
5-
j = ord(c) - ord("a")
6-
if d[j] and i - d[j] != distance[j]:
3+
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')]:
76
return False
8-
d[j] = i + 1
7+
d[c] = i
98
return True
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
function checkDistances(s: string, distance: number[]): boolean {
22
const n = s.length;
3-
const d = new Array(26).fill(0);
4-
for (let i = 0; i < n; i++) {
5-
const j = s[i].charCodeAt(0) - 'a'.charCodeAt(0);
6-
if (d[j] > 0 && i - d[j] !== distance[j]) {
3+
const d: number[] = new Array(26).fill(0);
4+
for (let i = 1; i <= n; ++i) {
5+
const j = s.charCodeAt(i - 1) - 97;
6+
if (d[j] && i - d[j] - 1 != distance[j]) {
77
return false;
88
}
9-
d[j] = i + 1;
9+
d[j] = i;
1010
}
1111
return true;
1212
}

0 commit comments

Comments
 (0)