5740:所有元音按顺序排布的最长子字符串
This commit is contained in:
parent
dd782d66b1
commit
749040b84c
@ -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)
|
||||||
|
|
||||||
|
}
|
@ -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>
|
Loading…
Reference in New Issue
Block a user