OJ 1794. 【软件认证】最长的指定瑕疵度的元音子串
OJ 1794. 【软件认证】最长的指定瑕疵度的元音子串
package ahwoj;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
public class YuanYinString1794 {
public static int getLongestFlawedVowelSubstrLen(String str, int mix) {
int cnt = 0;
String yuan = "aeiouAEIOU";
char[] chars = str.toCharArray();
int l = 0;
int r = 0;
int max = 0;
while(l < str.length() && r < str.length()) {
if (yuan.indexOf(chars[l]) == -1) {
if (cnt == mix) {
cnt--;
}
if (l == r) {
r++;
}
l++;
continue;
}
if (cnt == mix) {
if (yuan.indexOf(chars[r]) != -1) {
max = Math.max(r-l, max);
r++;
} else {
l++;
}
continue;
}
if (yuan.indexOf(chars[r]) == -1) {
cnt++;
}
r++;
}
return max;
}
public static int getLongestFlawedVowelSubstrLen2(String str, int mix) {
Queue queue = new LinkedList<Character>();
String yuan = "aeiouAEIOU";
char[] chars = str.toCharArray();
int cnt = 0;
int max = 0;
Set<Character> set = new HashSet<>();
set.addAll(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
for (int i = 0; i < str.length(); i++) {
if (yuan.indexOf(chars[i]) != -1) {
queue.add(chars[i]);
if (cnt == mix) {
max = Math.max(max,queue.size());
}
} else {
if (queue.isEmpty()) {
continue;
}
if (cnt < mix) {
queue.add(chars[i]);
cnt++;
} else {
while (!queue.isEmpty() && (cnt == mix || !set.contains(queue.peek()))) {
if (!set.contains(queue.peek())) {
cnt--;
}
queue.poll();
}
queue.add(chars[i]);
cnt++;
}
}
}
return max;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
int mix = Integer.parseInt(s);
String str = scanner.nextLine();
int len = getLongestFlawedVowelSubstrLen2(str, mix);
System.out.println(len);
}
}