File tree Expand file tree Collapse file tree 5 files changed +138
-0
lines changed
solution/2300-2399/2399.Check Distances Between Same Letters Expand file tree Collapse file tree 5 files changed +138
-0
lines changed Original file line number Diff line number Diff line change @@ -132,10 +132,59 @@ func checkDistances(s string, distance []int) bool {
132
132
}
133
133
```
134
134
135
+ ### ** C**
136
+
137
+ ``` c
138
+ bool checkDistances (char * s, int * distance, int distanceSize) {
139
+ int n = strlen(s);
140
+ int d[ 26] = {0};
141
+ for (int i = 0; i < n; i++) {
142
+ int j = s[ i] - 'a';
143
+ if (d[ j] > 0 && i - d[ j] != distance[ j] ) {
144
+ return false;
145
+ }
146
+ d[ j] = i + 1;
147
+ }
148
+ return true;
149
+ }
150
+ ```
151
+
135
152
### **TypeScript**
136
153
137
154
```ts
155
+ function checkDistances(s: string, distance: number[]): boolean {
156
+ 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]) {
161
+ return false;
162
+ }
163
+ d[j] = i + 1;
164
+ }
165
+ return true;
166
+ }
167
+ ```
138
168
169
+ ### ** Rust**
170
+
171
+ ``` rust
172
+ impl Solution {
173
+ pub fn check_distances (s : String , distance : Vec <i32 >) -> bool {
174
+ let n = s . len ();
175
+ let s = s . as_bytes ();
176
+ let mut d = [0 ; 26 ];
177
+ for i in 0 .. n {
178
+ let j = (s [i ] - b 'a' ) as usize ;
179
+ let i = i as i32 ;
180
+ if d [j ] > 0 && i - d [j ] != distance [j ] {
181
+ return false ;
182
+ }
183
+ d [j ] = i + 1 ;
184
+ }
185
+ true
186
+ }
187
+ }
139
188
```
140
189
141
190
### ** ...**
Original file line number Diff line number Diff line change @@ -118,10 +118,59 @@ func checkDistances(s string, distance []int) bool {
118
118
}
119
119
```
120
120
121
+ ### ** C**
122
+
123
+ ``` c
124
+ bool checkDistances (char * s, int * distance, int distanceSize) {
125
+ int n = strlen(s);
126
+ int d[ 26] = {0};
127
+ for (int i = 0; i < n; i++) {
128
+ int j = s[ i] - 'a';
129
+ if (d[ j] > 0 && i - d[ j] != distance[ j] ) {
130
+ return false;
131
+ }
132
+ d[ j] = i + 1;
133
+ }
134
+ return true;
135
+ }
136
+ ```
137
+
121
138
### **TypeScript**
122
139
123
140
```ts
141
+ function checkDistances(s: string, distance: number[]): boolean {
142
+ 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]) {
147
+ return false;
148
+ }
149
+ d[j] = i + 1;
150
+ }
151
+ return true;
152
+ }
153
+ ```
124
154
155
+ ### ** Rust**
156
+
157
+ ``` rust
158
+ impl Solution {
159
+ pub fn check_distances (s : String , distance : Vec <i32 >) -> bool {
160
+ let n = s . len ();
161
+ let s = s . as_bytes ();
162
+ let mut d = [0 ; 26 ];
163
+ for i in 0 .. n {
164
+ let j = (s [i ] - b 'a' ) as usize ;
165
+ let i = i as i32 ;
166
+ if d [j ] > 0 && i - d [j ] != distance [j ] {
167
+ return false ;
168
+ }
169
+ d [j ] = i + 1 ;
170
+ }
171
+ true
172
+ }
173
+ }
125
174
```
126
175
127
176
### ** ...**
Original file line number Diff line number Diff line change
1
+ bool checkDistances (char * s , int * distance , int distanceSize ) {
2
+ int n = strlen (s );
3
+ int d [26 ] = {0 };
4
+ for (int i = 0 ; i < n ; i ++ ) {
5
+ int j = s [i ] - 'a' ;
6
+ if (d [j ] > 0 && i - d [j ] != distance [j ]) {
7
+ return false;
8
+ }
9
+ d [j ] = i + 1 ;
10
+ }
11
+ return true;
12
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn check_distances ( s : String , distance : Vec < i32 > ) -> bool {
3
+ let n = s. len ( ) ;
4
+ let s = s. as_bytes ( ) ;
5
+ let mut d = [ 0 ; 26 ] ;
6
+ for i in 0 ..n {
7
+ let j = ( s[ i] - b'a' ) as usize ;
8
+ let i = i as i32 ;
9
+ if d[ j] > 0 && i - d[ j] != distance[ j] {
10
+ return false ;
11
+ }
12
+ d[ j] = i + 1 ;
13
+ }
14
+ true
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ function checkDistances ( s : string , distance : number [ ] ) : boolean {
2
+ 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 ] ) {
7
+ return false ;
8
+ }
9
+ d [ j ] = i + 1 ;
10
+ }
11
+ return true ;
12
+ }
You can’t perform that action at this time.
0 commit comments