OJ1794. 【软件认证】最长的指定瑕疵度的元音子串

OJ 1794. 【软件认证】最长的指定瑕疵度的元音子串

OJ 1794. 【软件认证】最长的指定瑕疵度的元音子串
在这里插入图片描述

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */

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;

/**
 * 开头和结尾都是元音字母(aeiouAEIOU)的字符串为 元音字符串,其中混杂的非元音字母数量为瑕疵度;
 * Diff1793的进阶题
 * eg: asdbuiodevauufgh 0
 */
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++; // 左移到非元音时,cnt--;
                }
                continue;
            }
            // l找到元音了,继续找r
            if (yuan.indexOf(chars[r]) == -1) { // 不是元音
                cnt++;
            }
            r++;
        }
        return max;
    }

    // dioufgiug Faeae  3
    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 {  // dioufgiug Faeae  3
                    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);
    }

}

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值