55
55
56
56
** 方法一:数组或哈希表**
57
57
58
- 我们可以用哈希表 $d$ 记录每个字母出现的下标,然后遍历哈希表,判断每个字母的下标之差是否等于 ` distance ` 中对应的值。
58
+ 我们可以用哈希表 $d$ 记录每个字母出现的下标,然后遍历哈希表,判断每个字母的下标之差是否等于 ` distance ` 中对应的值。如果出现不等的情况,直接返回 ` false ` 。如果遍历结束后,没有出现不等的情况,返回 ` true ` 。
59
59
60
- 时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。
60
+ 时间复杂度 $O(n)$,空间复杂度 $O(C)$。 其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字符集大小,本题中 $C = 26$ 。
61
61
62
62
<!-- tabs:start -->
63
63
68
68
``` python
69
69
class Solution :
70
70
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' )]:
75
74
return False
76
- d[j ] = i + 1
75
+ d[c ] = i
77
76
return True
78
77
```
79
78
@@ -85,12 +84,12 @@ class Solution:
85
84
class Solution {
86
85
public boolean checkDistances (String s , int [] distance ) {
87
86
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]) {
91
90
return false ;
92
91
}
93
- d[j] = i + 1 ;
92
+ d[j] = i;
94
93
}
95
94
return true ;
96
95
}
@@ -103,13 +102,13 @@ class Solution {
103
102
class Solution {
104
103
public:
105
104
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] ) {
110
109
return false;
111
110
}
112
- d[ j] = i + 1 ;
111
+ d[ j] = i;
113
112
}
114
113
return true;
115
114
}
@@ -120,13 +119,13 @@ public:
120
119
121
120
```go
122
121
func checkDistances(s string, distance []int) bool {
123
- d := make([ ]int, 26)
122
+ d := [26 ]int{}
124
123
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 ] {
127
126
return false
128
127
}
129
- d[j ] = i + 1
128
+ d[c ] = i + 1
130
129
}
131
130
return true
132
131
}
@@ -154,13 +153,13 @@ bool checkDistances(char *s, int *distance, int distanceSize) {
154
153
```ts
155
154
function checkDistances(s: string, distance: number[]): boolean {
156
155
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]) {
161
160
return false;
162
161
}
163
- d[j] = i + 1 ;
162
+ d[j] = i;
164
163
}
165
164
return true;
166
165
}
0 commit comments