Skip to content

Commit d066649

Browse files
committed
feat: add solutions to lc problem: No.2399
No.2399.Check Distances Between Same Letters
1 parent 70d3537 commit d066649

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,59 @@ func checkDistances(s string, distance []int) bool {
132132
}
133133
```
134134

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+
135152
### **TypeScript**
136153
137154
```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+
```
138168

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+
}
139188
```
140189

141190
### **...**

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,59 @@ func checkDistances(s string, distance []int) bool {
118118
}
119119
```
120120

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+
121138
### **TypeScript**
122139
123140
```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+
```
124154

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+
}
125174
```
126175

127176
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

0 commit comments

Comments
 (0)