Skip to content

Commit 068e26f

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

File tree

6 files changed

+221
-2
lines changed

6 files changed

+221
-2
lines changed

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

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,103 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:数组/哈希表**
56+
57+
用数组或哈希表记录每个字符第一次出现的位置。
58+
59+
遍历字符串每个字符 $c$,若 $c$ 在数组中的值为 $-1$,则更新为当前位置,否则答案更新为当前位置与数组中的值的差值的最大值。
60+
61+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串长度,而 $C$ 为字符集大小。
62+
5563
<!-- tabs:start -->
5664

5765
### **Python3**
5866

5967
<!-- 这里可写当前语言的特殊实现逻辑 -->
6068

6169
```python
62-
70+
class Solution:
71+
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
72+
d = {}
73+
ans = -1
74+
for i, c in enumerate(s):
75+
if c in d:
76+
ans = max(ans, i - d[c] - 1)
77+
else:
78+
d[c] = i
79+
return ans
6380
```
6481

6582
### **Java**
6683

6784
<!-- 这里可写当前语言的特殊实现逻辑 -->
6885

6986
```java
87+
class Solution {
88+
public int maxLengthBetweenEqualCharacters(String s) {
89+
int[] d = new int[26];
90+
Arrays.fill(d, -1);
91+
int ans = -1;
92+
for (int i = 0; i < s.length(); ++i) {
93+
int j = s.charAt(i) - 'a';
94+
if (d[j] == -1) {
95+
d[j] = i;
96+
} else {
97+
ans = Math.max(ans, i - d[j] - 1);
98+
}
99+
}
100+
return ans;
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int maxLengthBetweenEqualCharacters(string s) {
111+
vector<int> d(26, -1);
112+
int ans = -1;
113+
for (int i = 0; i < s.size(); ++i) {
114+
int j = s[i] - 'a';
115+
if (d[j] == -1) {
116+
d[j] = i;
117+
} else {
118+
ans = max(ans, i - d[j] - 1);
119+
}
120+
}
121+
return ans;
122+
}
123+
};
124+
```
70125
126+
### **Go**
127+
128+
```go
129+
func maxLengthBetweenEqualCharacters(s string) int {
130+
d := make([]int, 26)
131+
for i := range d {
132+
d[i] = -1
133+
}
134+
ans := -1
135+
for i, c := range s {
136+
c -= 'a'
137+
if d[c] == -1 {
138+
d[c] = i
139+
} else {
140+
ans = max(ans, i-d[c]-1)
141+
}
142+
}
143+
return ans
144+
}
145+
146+
func max(a, b int) int {
147+
if a > b {
148+
return a
149+
}
150+
return b
151+
}
71152
```
72153

73154
### **...**

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,86 @@
4747
### **Python3**
4848

4949
```python
50-
50+
class Solution:
51+
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
52+
d = {}
53+
ans = -1
54+
for i, c in enumerate(s):
55+
if c in d:
56+
ans = max(ans, i - d[c] - 1)
57+
else:
58+
d[c] = i
59+
return ans
5160
```
5261

5362
### **Java**
5463

5564
```java
65+
class Solution {
66+
public int maxLengthBetweenEqualCharacters(String s) {
67+
int[] d = new int[26];
68+
Arrays.fill(d, -1);
69+
int ans = -1;
70+
for (int i = 0; i < s.length(); ++i) {
71+
int j = s.charAt(i) - 'a';
72+
if (d[j] == -1) {
73+
d[j] = i;
74+
} else {
75+
ans = Math.max(ans, i - d[j] - 1);
76+
}
77+
}
78+
return ans;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int maxLengthBetweenEqualCharacters(string s) {
89+
vector<int> d(26, -1);
90+
int ans = -1;
91+
for (int i = 0; i < s.size(); ++i) {
92+
int j = s[i] - 'a';
93+
if (d[j] == -1) {
94+
d[j] = i;
95+
} else {
96+
ans = max(ans, i - d[j] - 1);
97+
}
98+
}
99+
return ans;
100+
}
101+
};
102+
```
56103
104+
### **Go**
105+
106+
```go
107+
func maxLengthBetweenEqualCharacters(s string) int {
108+
d := make([]int, 26)
109+
for i := range d {
110+
d[i] = -1
111+
}
112+
ans := -1
113+
for i, c := range s {
114+
c -= 'a'
115+
if d[c] == -1 {
116+
d[c] = i
117+
} else {
118+
ans = max(ans, i-d[c]-1)
119+
}
120+
}
121+
return ans
122+
}
123+
124+
func max(a, b int) int {
125+
if a > b {
126+
return a
127+
}
128+
return b
129+
}
57130
```
58131

59132
### **...**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int maxLengthBetweenEqualCharacters(string s) {
4+
vector<int> d(26, -1);
5+
int ans = -1;
6+
for (int i = 0; i < s.size(); ++i) {
7+
int j = s[i] - 'a';
8+
if (d[j] == -1) {
9+
d[j] = i;
10+
} else {
11+
ans = max(ans, i - d[j] - 1);
12+
}
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func maxLengthBetweenEqualCharacters(s string) int {
2+
d := make([]int, 26)
3+
for i := range d {
4+
d[i] = -1
5+
}
6+
ans := -1
7+
for i, c := range s {
8+
c -= 'a'
9+
if d[c] == -1 {
10+
d[c] = i
11+
} else {
12+
ans = max(ans, i-d[c]-1)
13+
}
14+
}
15+
return ans
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int maxLengthBetweenEqualCharacters(String s) {
3+
int[] d = new int[26];
4+
Arrays.fill(d, -1);
5+
int ans = -1;
6+
for (int i = 0; i < s.length(); ++i) {
7+
int j = s.charAt(i) - 'a';
8+
if (d[j] == -1) {
9+
d[j] = i;
10+
} else {
11+
ans = Math.max(ans, i - d[j] - 1);
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
3+
d = {}
4+
ans = -1
5+
for i, c in enumerate(s):
6+
if c in d:
7+
ans = max(ans, i - d[c] - 1)
8+
else:
9+
d[c] = i
10+
return ans

0 commit comments

Comments
 (0)