最长的指定瑕疵度的元音子串

标题:最长的指定瑕疵度的元音子串 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限

开头和结尾都是元音字母(aeiouAEIOU)的字符串为 元音字符串 ,其中混杂的非元音字母数量为其 瑕疵度 。比如:


def calc(flaw, src):
    res = 0
    for i in range(len(src)):
        count = 0
        if src[i] not in "aeiouAEIOU":
            continue
        for j in range(i + 1, len(src)):
            if src[j] not in "aeiouAEIOU":
                count += 1
            else:
                if count > flaw:
                    break
                if count == flaw and res < j - i + 1:
                    res = j - i + 1
    return res

def Input():
    flaw = int(input())
    src = input()
    count = 0
    for i in src:
        if i in "aeiouAEIOU":
            count += 1
    if flaw == 0 and count == 1:
        return 1
    res = calc(flaw, src)
    return res

print(Input())

import java.util.*;

public class Main {
    private static HashSet<Character> hashSet = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int flaw = sc.nextInt();
        sc.nextLine();
        String input = sc.nextLine();

        int left = 0;
        int right = -1;
        int currentFlaw = 0;
        int max = 0;

        while (right < input.length() - 1) {
            right++;
            if (!isChar(input.charAt(right))) {
                currentFlaw++;
            }
            while (currentFlaw > flaw) {
                if (!isChar(input.charAt(left))) {
                    currentFlaw--;
                }
                left++;
            }
            if (left < input.length() && currentFlaw == flaw && isChar(input.charAt(left)) && isChar(input.charAt(right))) {
                max = Math.max(max, right + 1 - left);
            }
        }
        System.out.println(max);
    }

    private static boolean isChar(char c) {
        return hashSet.contains(c);
    }
}//manfen


全部评论

相关推荐

点赞 1 评论
分享
牛客网
牛客企业服务