Skip to content

Commit fe153ab

Browse files
authored
Merge pull request #1323 from 0xff-dev/1839
Add solution and test-cases for problem 1839
2 parents 62eddfb + a9a270c commit fe153ab

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [1839.Longest Substring Of All Vowels in Order][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+
A string is considered **beautiful** if it satisfies the following conditions:
5+
6+
- Each of the 5 English vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`) must appear **at least once** in it.
7+
- The letters must be sorted in **alphabetical order** (i.e. all `'a'`s before `'e'`s, all `'e'`s before `'i'`s, etc.).
8+
9+
For example, strings `"aeiou"` and `"aaaaaaeiiiioou"` are considered **beautiful**, but `"uaeio"`, `"aeoiu"`, and `"aaaeeeooo"` are **not beautiful**.
10+
11+
Given a string `word` consisting of English vowels, return the **length of the longest beautiful substring** of `word`. If no such substring exists, return `0`.
12+
13+
A **substring** is a contiguous sequence of characters in a string.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
19+
Output: 13
20+
Explanation: The longest beautiful substring in word is "aaaaeiiiiouuu" of length 13.
1321
```
1422

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

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Longest Substring Of All Vowels in Order
23-
```go
2425
```
26+
Input: word = "aeeeiiiioooauuuaeiou"
27+
Output: 5
28+
Explanation: The longest beautiful substring in word is "aeiou" of length 5.
29+
```
30+
31+
**Example 3:**
2532

33+
```
34+
Input: word = "a"
35+
Output: 0
36+
Explanation: There is no beautiful substring, so return 0.
37+
```
2638

2739
## 结语
2840

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

3-
func Solution(x bool) bool {
4-
return x
3+
type charCount struct {
4+
char byte
5+
count int
6+
}
7+
8+
func Solution(word string) int {
9+
10+
pre := byte(' ')
11+
// a, e, i, o, u
12+
group := make([]charCount, 0)
13+
index := -1
14+
for _, b := range []byte(word) {
15+
if b != pre {
16+
group = append(group, charCount{b, 1})
17+
index++
18+
pre = b
19+
continue
20+
}
21+
group[index].count++
22+
}
23+
ret := 0
24+
// 1,2,3,4,5
25+
for i := 0; i <= len(group)-5; i++ {
26+
if group[i].char == 'a' && group[i+1].char == 'e' && group[i+2].char == 'i' && group[i+3].char == 'o' && group[i+4].char == 'u' {
27+
ret = max(ret, group[i].count+group[i+1].count+group[i+2].count+group[i+3].count+group[i+4].count)
28+
}
29+
}
30+
return ret
531
}

leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/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", "aeiaaioaaaaeiiiiouuuooaauuaeiu", 13},
17+
{"TestCase2", "aeeeiiiioooauuuaeiou", 5},
18+
{"TestCase3", "a", 0},
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)