diff --git a/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/README.md b/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/README.md index 0795ff28c..d2fbcbabf 100755 --- a/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/README.md +++ b/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/README.md @@ -1,28 +1,40 @@ # [1839.Longest Substring Of All Vowels in Order][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +A string is considered **beautiful** if it satisfies the following conditions: + +- Each of the 5 English vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`) must appear **at least once** in it. +- The letters must be sorted in **alphabetical order** (i.e. all `'a'`s before `'e'`s, all `'e'`s before `'i'`s, etc.). + +For example, strings `"aeiou"` and `"aaaaaaeiiiioou"` are considered **beautiful**, but `"uaeio"`, `"aeoiu"`, and `"aaaeeeooo"` are **not beautiful**. + +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`. + +A **substring** is a contiguous sequence of characters in a string. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: word = "aeiaaioaaaaeiiiiouuuooaauuaeiu" +Output: 13 +Explanation: The longest beautiful substring in word is "aaaaeiiiiouuu" of length 13. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Longest Substring Of All Vowels in Order -```go ``` +Input: word = "aeeeiiiioooauuuaeiou" +Output: 5 +Explanation: The longest beautiful substring in word is "aeiou" of length 5. +``` + +**Example 3:** +``` +Input: word = "a" +Output: 0 +Explanation: There is no beautiful substring, so return 0. +``` ## 结语 diff --git a/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/Solution.go b/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/Solution.go index d115ccf5e..44be239c9 100644 --- a/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/Solution.go +++ b/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/Solution.go @@ -1,5 +1,31 @@ package Solution -func Solution(x bool) bool { - return x +type charCount struct { + char byte + count int +} + +func Solution(word string) int { + + pre := byte(' ') + // a, e, i, o, u + group := make([]charCount, 0) + index := -1 + for _, b := range []byte(word) { + if b != pre { + group = append(group, charCount{b, 1}) + index++ + pre = b + continue + } + group[index].count++ + } + ret := 0 + // 1,2,3,4,5 + for i := 0; i <= len(group)-5; i++ { + 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' { + ret = max(ret, group[i].count+group[i+1].count+group[i+2].count+group[i+3].count+group[i+4].count) + } + } + return ret } diff --git a/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/Solution_test.go b/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/Solution_test.go index 14ff50eb4..cfc00257a 100644 --- a/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/Solution_test.go +++ b/leetcode/1801-1900/1839.Longest-Substring-Of-All-Vowels-in-Order/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "aeiaaioaaaaeiiiiouuuooaauuaeiu", 13}, + {"TestCase2", "aeeeiiiioooauuuaeiou", 5}, + {"TestCase3", "a", 0}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }