5740:所有元音按顺序排布的最长子字符串

This commit is contained in:
huangge1199 2021-04-25 14:21:33 +08:00
parent dd782d66b1
commit 749040b84c
2 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,111 @@
//当一个字符串满足如下条件时我们称它是 美丽的
//
//
// 所有 5 个英文元音字母'a' 'e' 'i' 'o' 'u'都必须 至少 出现一次
// 这些元音字母的顺序都必须按照 字典序 升序排布也就是说所有的 'a' 都在 'e' 前面所有的 'e' 都在 'i' 前面以此类推
//
//
// 比方说字符串 "aeiou" "aaaaaaeiiiioou" 都是 美丽的 但是 "uaeio" "aeoiu" "aaaeeeooo"
//不是美丽的
//
// 给你一个只包含英文元音字母的字符串 word 请你返回 word 最长美丽子字符串的长度 如果不存在这样的子字符串请返回 0
//
// 子字符串 是字符串中一个连续的字符序列
//
//
//
// 示例 1
//
//
//输入word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
//输出13
//解释最长子字符串是 "aaaaeiiiiouuu" 长度为 13
//
// 示例 2
//
//
//输入word = "aeeeiiiioooauuuaeiou"
//输出5
//解释最长子字符串是 "aeiou" 长度为 5
//
//
// 示例 3
//
//
//输入word = "a"
//输出0
//解释没有美丽子字符串所以返回 0
//
//
//
//
// 提示
//
//
// 1 <= word.length <= 5 * 105
// word 只包含字符 'a''e''i''o' 'u'
//
// Related Topics 双指针 字符串
// 👍 1 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.List;
//5740:所有元音按顺序排布的最长子字符串
public class LongestSubstringOfAllVowelsInOrder {
public static void main(String[] args) {
//测试代码
Solution solution = new LongestSubstringOfAllVowelsInOrder().new Solution();
//13
System.out.println(solution.longestBeautifulSubstring("aeiaaioaaaaeiiiiouuuooaauuaeiu"));
//5
System.out.println(solution.longestBeautifulSubstring("aeeeiiiioooauuuaeiou"));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int longestBeautifulSubstring(String word) {
if (word.length() < 5) {
return 0;
}
int max = 0;
int index = word.indexOf("a");
List<Character> list = Arrays.asList('a', 'e', 'i', 'o', 'u');
while (index >= 0) {
int num = 1;
int flag = 0;
int i;
for (i = index + 1; i < word.length(); i++) {
if (word.charAt(i) == list.get(flag)) {
num++;
} else if (flag != list.size() - 1 && word.charAt(i) == list.get(flag + 1)) {
num++;
flag++;
} else {
if (list.get(flag) == 'u') {
max = Math.max(num, max);
}
if (word.substring(index + num).contains("a")) {
index = index + num + word.substring(index + num).indexOf("a");
} else {
index = -1;
}
break;
}
}
if (i == word.length()) {
if (list.get(flag) == 'u') {
max = Math.max(num, max);
}
break;
}
}
return max;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,47 @@
<p>当一个字符串满足如下条件时,我们称它是 <b>美丽的</b> </p>
<ul>
<li>所有 5 个英文元音字母(<code>'a'</code> <code>'e'</code> <code>'i'</code> <code>'o'</code> <code>'u'</code>)都必须 <strong>至少</strong> 出现一次。</li>
<li>这些元音字母的顺序都必须按照 <strong>字典序</strong> 升序排布(也就是说所有的 <code>'a'</code> 都在 <code>'e'</code> 前面,所有的 <code>'e'</code> 都在 <code>'i'</code> 前面,以此类推)</li>
</ul>
<p>比方说,字符串 <code>"aeiou"</code> 和 <code>"aaaaaaeiiiioou"</code> 都是 <strong>美丽的</strong> ,但是 <code>"uaeio"</code> <code>"aeoiu"</code> 和 <code>"aaaeeeooo"</code> <strong>不是美丽的</strong> 。</p>
<p>给你一个只包含英文元音字母的字符串 <code>word</code> ,请你返回 <code>word</code><strong>最长美丽子字符串的长度</strong> 。如果不存在这样的子字符串,请返回 <code>0</code> 。</p>
<p><strong>子字符串</strong> 是字符串中一个连续的字符序列。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>word = "aeiaaio<strong>aaaaeiiiiouuu</strong>ooaauuaeiu"
<b>输出:</b>13
<b>解释:</b>最长子字符串是 "aaaaeiiiiouuu" ,长度为 13 。</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>word = "aeeeiiiioooauuu<strong>aeiou</strong>"
<b>输出:</b>5
<b>解释:</b>最长子字符串是 "aeiou" ,长度为 5 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>word = "a"
<b>输出:</b>0
<b>解释:</b>没有美丽子字符串,所以返回 0 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= word.length <= 5 * 10<sup>5</sup></code></li>
<li><code>word</code> 只包含字符 <code>'a'</code><code>'e'</code><code>'i'</code><code>'o'</code> 和 <code>'u'</code> 。</li>
</ul>
<div><div>Related Topics</div><div><li>双指针</li><li>字符串</li></div></div>\n<div><li>👍 1</li><li>👎 0</li></div>