Skip to content

Commit e0fa19d

Browse files
committed
feat: add solutions to lc problem: No.1624
No.1624.Largest Substring Between Two Equal Characters
1 parent ab085b9 commit e0fa19d

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

solution/1600-1699/1624.Largest Substring Between Two Equal Characters/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,71 @@ func max(a, b int) int {
151151
}
152152
```
153153

154+
### **C**
155+
156+
```c
157+
#define max(a,b) (((a) > (b)) ? (a) : (b))
158+
159+
int maxLengthBetweenEqualCharacters(char *s) {
160+
int pos[26];
161+
memset(pos, -1, sizeof(pos));
162+
int n = strlen(s);
163+
int res = -1;
164+
for (int i = 0; i < n; i++) {
165+
char c = s[i];
166+
int j = c - 'a';
167+
if (pos[j] == -1) {
168+
pos[j] = i;
169+
} else {
170+
res = max(res, i - pos[j] - 1);
171+
}
172+
}
173+
return res;
174+
}
175+
```
176+
177+
### **TypeScript**
178+
179+
```ts
180+
function maxLengthBetweenEqualCharacters(s: string): number {
181+
const n = s.length;
182+
const pos = new Array(26).fill(-1);
183+
let res = -1;
184+
for (let i = 0; i < n; i++) {
185+
const j = s[i].charCodeAt(0) - 'a'.charCodeAt(0);
186+
if (pos[j] === -1) {
187+
pos[j] = i;
188+
} else {
189+
res = Math.max(res, i - pos[j] - 1);
190+
}
191+
}
192+
return res;
193+
}
194+
```
195+
196+
### **Rust**
197+
198+
```rust
199+
impl Solution {
200+
pub fn max_length_between_equal_characters(s: String) -> i32 {
201+
let s = s.as_bytes();
202+
let n = s.len();
203+
let mut pos = [-1; 26];
204+
let mut res = -1;
205+
for i in 0..n {
206+
let j = (s[i] - b'a') as usize;
207+
let i = i as i32;
208+
if pos[j] == -1 {
209+
pos[j] = i;
210+
} else {
211+
res = res.max(i - pos[j] - 1);
212+
}
213+
}
214+
res
215+
}
216+
}
217+
```
218+
154219
### **...**
155220

156221
```

solution/1600-1699/1624.Largest Substring Between Two Equal Characters/README_EN.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,71 @@ func max(a, b int) int {
129129
}
130130
```
131131

132+
### **C**
133+
134+
```c
135+
#define max(a,b) (((a) > (b)) ? (a) : (b))
136+
137+
int maxLengthBetweenEqualCharacters(char *s) {
138+
int pos[26];
139+
memset(pos, -1, sizeof(pos));
140+
int n = strlen(s);
141+
int res = -1;
142+
for (int i = 0; i < n; i++) {
143+
char c = s[i];
144+
int j = c - 'a';
145+
if (pos[j] == -1) {
146+
pos[j] = i;
147+
} else {
148+
res = max(res, i - pos[j] - 1);
149+
}
150+
}
151+
return res;
152+
}
153+
```
154+
155+
### **TypeScript**
156+
157+
```ts
158+
function maxLengthBetweenEqualCharacters(s: string): number {
159+
const n = s.length;
160+
const pos = new Array(26).fill(-1);
161+
let res = -1;
162+
for (let i = 0; i < n; i++) {
163+
const j = s[i].charCodeAt(0) - 'a'.charCodeAt(0);
164+
if (pos[j] === -1) {
165+
pos[j] = i;
166+
} else {
167+
res = Math.max(res, i - pos[j] - 1);
168+
}
169+
}
170+
return res;
171+
}
172+
```
173+
174+
### **Rust**
175+
176+
```rust
177+
impl Solution {
178+
pub fn max_length_between_equal_characters(s: String) -> i32 {
179+
let s = s.as_bytes();
180+
let n = s.len();
181+
let mut pos = [-1; 26];
182+
let mut res = -1;
183+
for i in 0..n {
184+
let j = (s[i] - b'a') as usize;
185+
let i = i as i32;
186+
if pos[j] == -1 {
187+
pos[j] = i;
188+
} else {
189+
res = res.max(i - pos[j] - 1);
190+
}
191+
}
192+
res
193+
}
194+
}
195+
```
196+
132197
### **...**
133198

134199
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define max(a,b) (((a) > (b)) ? (a) : (b))
2+
3+
int maxLengthBetweenEqualCharacters(char *s) {
4+
int pos[26];
5+
memset(pos, -1, sizeof(pos));
6+
int n = strlen(s);
7+
int res = -1;
8+
for (int i = 0; i < n; i++) {
9+
char c = s[i];
10+
int j = c - 'a';
11+
if (pos[j] == -1) {
12+
pos[j] = i;
13+
} else {
14+
res = max(res, i - pos[j] - 1);
15+
}
16+
}
17+
return res;
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn max_length_between_equal_characters(s: String) -> i32 {
3+
let s = s.as_bytes();
4+
let n = s.len();
5+
let mut pos = [-1; 26];
6+
let mut res = -1;
7+
for i in 0..n {
8+
let j = (s[i] - b'a') as usize;
9+
let i = i as i32;
10+
if pos[j] == -1 {
11+
pos[j] = i;
12+
} else {
13+
res = res.max(i - pos[j] - 1);
14+
}
15+
}
16+
res
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxLengthBetweenEqualCharacters(s: string): number {
2+
const n = s.length;
3+
const pos = new Array(26).fill(-1);
4+
let res = -1;
5+
for (let i = 0; i < n; i++) {
6+
const j = s[i].charCodeAt(0) - 'a'.charCodeAt(0);
7+
if (pos[j] === -1) {
8+
pos[j] = i;
9+
} else {
10+
res = Math.max(res, i - pos[j] - 1);
11+
}
12+
}
13+
return res;
14+
}

0 commit comments

Comments
 (0)