8000 Merge pull request #728 from 0xff-dev/1624 · 6boris/awesome-golang-algorithm@c8605a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit c8605a9

Browse files
authored
Merge pull request #728 from 0xff-dev/1624
Add solution and test-cases for problem 1624
2 parents 165d3b0 + 96d2632 commit c8605a9

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

leetcode/1601-1700/1624.Largest-Substring-Between-Two-Equal-Characters/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [1624.Largest Substring Between Two Equal Characters][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a string `s`, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return `-1`.
5+
6+
A **substring** is a contiguous sequence of characters within a string.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: s = "aa"
12+
Output: 0
13+
Explanation: The optimal substring here is an empty substring between the two 'a's.
1314
```
1415

15-
## 题意
16-
> ...
16+
**Example 2:**
1717

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Largest Substring Between Two Equal Characters
23-
```go
18+
```
19+
Input: s = "abca"
20+
Output: 2
21+
Explanation: The optimal substring here is "bc".
2422
```
2523

24+
**Example 3:**
25+
26+
```
27+
Input: s = "cbzxy"
28+
Output: -1
29+
Explanation: There are no characters that appear twice in s.
30+
```
2631

2732
## 结语
2833

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
m1 := [26]int{}
5+
m2 := [26]int{}
6+
for i := 0; i < 26; i++ {
7+
m1[i] = -1
8+
m2[i] = -1
9+
}
10+
for idx, b := range s {
11+
if m1[b-'a'] == -1 {
12+
m1[b-'a'] = idx
13+
}
14+
}
15+
for idx := len(s) - 1; idx >= 0; idx-- {
16+
if m2[s[idx]-'a'] == -1 {
17+
m2[s[idx]-'a'] = idx
18+
}
19+
}
20+
ans := -1
21+
for i := 0; i < 26; i++ {
22+
if m1[i] != -1 && m2[i] != -1 && m2[i] != m1[i] {
23+
if diff := m2[i] - m1[i] - 1; diff > ans {
24+
ans = diff
25+
}
26+
}
27+
}
28+
return ans
529
}

leetcode/1601-1700/1624.Largest-Substring-Between-Two-Equal-Characters/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "aa", 0},
17+
{"TestCase2", "abca", 2},
18+
{"TestCase3", "cbzxy", -1},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)
0