Skip to content

Commit a4f1bd8

Browse files
committed
一刷1839
1 parent 1f4ec14 commit a4f1bd8

File tree

6 files changed

+105
-47
lines changed

6 files changed

+105
-47
lines changed

README.adoc

+6-6
Original file line numberDiff line numberDiff line change
@@ -12899,12 +12899,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
1289912899
//|Medium
1290012900
//|
1290112901

12902-
//|{counter:codes}
12903-
//|{leetcode_base_url}/longest-substring-of-all-vowels-in-order/[1839. Longest Substring Of All Vowels in Order^]
12904-
//|{source_base_url}/_1839_LongestSubstringOfAllVowelsInOrder.java[Java]
12905-
//|{doc_base_url}/1839-longest-substring-of-all-vowels-in-order.adoc[题解]
12906-
//|Medium
12907-
//|
12902+
|{counter:codes}
12903+
|{leetcode_base_url}/longest-substring-of-all-vowels-in-order/[1839. Longest Substring Of All Vowels in Order^]
12904+
|{source_base_url}/_1839_LongestSubstringOfAllVowelsInOrder.java[Java]
12905+
|{doc_base_url}/1839-longest-substring-of-all-vowels-in-order.adoc[题解]
12906+
|Medium
12907+
|
1290812908

1290912909
//|{counter:codes}
1291012910
//|{leetcode_base_url}/maximum-building-height/[1840. Maximum Building Height^]
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,57 @@
11
[#1839-longest-substring-of-all-vowels-in-order]
2-
= 1839. Longest Substring Of All Vowels in Order
2+
= 1839. 所有元音按顺序排布的最长子字符串
33

4-
{leetcode}/problems/longest-substring-of-all-vowels-in-order/[LeetCode - 1839. Longest Substring Of All Vowels in Order ^]
4+
https://leetcode.cn/problems/longest-substring-of-all-vowels-in-order/[LeetCode - 1839. 所有元音按顺序排布的最长子字符串 ^]
55

6-
A string is considered *beautiful* if it satisfies the following conditions:
6+
当一个字符串满足如下条件时,我们称它是 *美丽的*
77

8+
* 所有 5 个英文元音字母(`a``e``i``o``u`)都必须 *至少* 出现一次。
9+
* 这些元音字母的顺序都必须按照 *字典序* 升序排布(也就是说所有的 `a` 都在 `e` 前面,所有的 `e` 都在 `i` 前面,以此类推)
810
9-
* Each of the 5 English vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`) must appear *at least once* in it.
10-
* The letters must be sorted in *alphabetical order* (i.e. all `'a'`s before `'e'`s, all `'e'`s before `'i'`s, etc.).
11+
比方说,字符串 `aeiou``aaaaaaeiiiioou` 都是 *美丽的* ,但是 `uaeio``aeoiu``aaaeeeooo` *不是美丽的*
1112

13+
给你一个只包含英文元音字母的字符串 `word` ,请你返回 `word`*最长美丽子字符串的长度* 。如果不存在这样的子字符串,请返回 `0`
1214

13-
For example, strings `"aeiou"` and `"aaaaaaeiiiioou"` are considered *beautiful*, but `"uaeio"`, `"aeoiu"`, and `"aaaeeeooo"` are *not beautiful*.
15+
*子字符串* 是字符串中一个连续的字符序列。
1416

15-
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`.
17+
*示例 1:*
1618

17-
A *substring* is a contiguous sequence of characters in a string.
19+
....
20+
输入:word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
21+
输出:13
22+
解释:最长子字符串是 "aaaaeiiiiouuu" ,长度为 13 。
23+
....
1824

19-
20-
*Example 1:*
25+
*示例 2:*
2126

22-
[subs="verbatim,quotes"]
23-
----
24-
*Input:* word = "aeiaaio[.underline]#aaaaeiiiiouuu#ooaauuaeiu"
25-
*Output:* 13
26-
*Explanation:* The longest beautiful substring in word is "aaaaeiiiiouuu" of length 13.
27-
----
27+
....
28+
输入:word = "aeeeiiiioooauuuaeiou"
29+
输出:5
30+
解释:最长子字符串是 "aeiou" ,长度为 5 。
31+
....
2832

29-
*Example 2:*
33+
*示例 3:*
3034

31-
[subs="verbatim,quotes"]
32-
----
33-
*Input:* word = "aeeeiiiioooauuu[.underline]#aeiou#"
34-
*Output:* 5
35-
*Explanation:* The longest beautiful substring in word is "aeiou" of length 5.
35+
....
36+
输入:word = "a"
37+
输出:0
38+
解释:没有美丽子字符串,所以返回 0 。
39+
....
3640

37-
----
41+
*提示:*
3842

39-
*Example 3:*
43+
* `1 \<= word.length \<= 5 * 10^5^`
44+
* `word` 只包含字符 `a``e``i``o``u`
4045
41-
[subs="verbatim,quotes"]
42-
----
43-
*Input:* word = "a"
44-
*Output:* 0
45-
*Explanation:* There is no beautiful substring, so return 0.
46-
47-
----
48-
49-
50-
*Constraints:*
51-
52-
53-
* `1 <= word.length <= 5 * 10^5^`
54-
* `word` consists of characters `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.
5546
5647
48+
== 思路分析
5749

50+
其实就是一个字符判断,当遇到第一个 `a` 时开始计数,符合要求(和上一个字符相同,或者是下一个元音字符)计数加一,否则归零。当到达最后一个元音字符时,开始更新结果值,保留最大值。
5851

59-
== 思路分析
52+
image::images/1839-10.png[{image_attr}]
6053

54+
TIP: 看题解,可以只比较字符大小来解答,探索一下解法。
6155

6256
[[src-1839]]
6357
[tabs]
@@ -84,4 +78,6 @@ include::{sourcedir}/_1839_LongestSubstringOfAllVowelsInOrder.java[tag=answer]
8478

8579
== 参考资料
8680

87-
81+
. https://leetcode.cn/problems/longest-substring-of-all-vowels-in-order/solutions/742722/suo-you-yuan-yin-an-shun-xu-pai-bu-de-zu-9wqg/[1839. 所有元音按顺序排布的最长子字符串 - 官方题解^]
82+
. https://leetcode.cn/problems/longest-substring-of-all-vowels-in-order/solutions/742704/bi-da-xiao-by-sweetpepperj-gdlt/[1839. 所有元音按顺序排布的最长子字符串 - 不用元音判断,直接通过比大小解题^] -- 这个解法非常轻巧!
83+
. https://leetcode.cn/problems/longest-substring-of-all-vowels-in-order/solutions/3674813/tan-xin-hua-dong-chuang-kou-c-by-lukasir-l7la/[1839. 所有元音按顺序排布的最长子字符串 - 贪心+滑动窗口(C++)^]

docs/images/1839-10.png

292 KB
Loading

docs/index.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3761,7 +3761,7 @@ include::1823-find-the-winner-of-the-circular-game.adoc[leveloffset=+1]
37613761

37623762
// include::1838-frequency-of-the-most-frequent-element.adoc[leveloffset=+1]
37633763

3764-
// include::1839-longest-substring-of-all-vowels-in-order.adoc[leveloffset=+1]
3764+
include::1839-longest-substring-of-all-vowels-in-order.adoc[leveloffset=+1]
37653765

37663766
// include::1840-maximum-building-height.adoc[leveloffset=+1]
37673767

logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,11 @@ endif::[]
678678
|{doc_base_url}/0409-longest-palindrome.adoc[题解]
679679
|✅ 回文
680680

681+
|{counter:codes2503}
682+
|{leetcode_base_url}/longest-substring-of-all-vowels-in-order/[1839. 所有元音按顺序排布的最长子字符串^]
683+
|{doc_base_url}/1839-longest-substring-of-all-vowels-in-order.adoc[题解]
684+
|✅ 滑动窗口。题目不难,只是处理边界条件麻烦。另外,也有取巧解法,可以探索一下。
685+
681686
|===
682687

683688
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _1839_LongestSubstringOfAllVowelsInOrder {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-13 21:48:41
9+
*/
10+
public int longestBeautifulSubstring(String word) {
11+
int result = 0;
12+
int cnt = 0;
13+
char[] chars = new char[]{'a', 'e', 'i', 'o', 'u'};
14+
int idx = -1;
15+
for (int i = 0; i < word.length(); i++) {
16+
char c = word.charAt(i);
17+
if (i == 0) {
18+
if (c == 'a') {
19+
cnt = 1;
20+
idx = 0;
21+
}
22+
} else {
23+
if (word.charAt(i - 1) > c) {
24+
if (c == 'a') {
25+
cnt = 1;
26+
idx = 0;
27+
} else {
28+
cnt = 0;
29+
idx = -1;
30+
}
31+
} else {
32+
if (idx >= 0) {
33+
if (c == chars[idx]) {
34+
cnt++;
35+
} else if (idx < chars.length - 1 && c == chars[idx + 1]) {
36+
idx++;
37+
cnt++;
38+
} else {
39+
cnt = 0;
40+
idx = -1;
41+
}
42+
}
43+
}
44+
}
45+
if (idx == chars.length - 1) {
46+
result = Math.max(result, cnt);
47+
}
48+
}
49+
return result;
50+
}
51+
52+
// end::answer[]
53+
public static void main(String[] args) {
54+
new _1839_LongestSubstringOfAllVowelsInOrder()
55+
.longestBeautifulSubstring("aeiaaioaaaaeiiiiouuuooaauuaeiu");
56+
}
57+
}

0 commit comments

Comments
 (0)