LeetCode 1371. Find the Longest Substring Containing Vowels in Even Counts

news/2024/10/8 22:51:13

原题链接在这里:https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/description/

题目:

Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times.

Example 1:

Input: s = "eleetminicoworoep"
Output: 13
Explanation: The longest substring is "leetminicowor" which contains two each of the vowels: e, i and o and zero of the vowels: a and u.

Example 2:

Input: s = "leetcodeisgreat"
Output: 5
Explanation: The longest substring is "leetc" which contains two e's.

Example 3:

Input: s = "bcbcbc"
Output: 6
Explanation: In this case, the given string "bcbcbc" is the longest because all vowels: a, e, i, o and u appear zero times.

Constraints:

  • 1 <= s.length <= 5 x 10^5
  • s contains only lowercase English letters.

题解:

We need to know if the vowel count is even. But we don't really need the count, we need to know count % 2 == 0.

Thus we could use ^.

We need a mask to maintain the current state.

We also need a map to maintain the state to its oldest index. If we see the same state, then it means this period from its oldest index has all the even vowels.

1 << (ind + 1) >> 1 since here if it is not vowek, ind is -1. 1<<0>>1 == 0. That is why we need ind + 1.
Time Complexity: O(n). n = s.length().
Space: O(1). At most 32 states.
AC Java:
 1 class Solution {
 2     public int findTheLongestSubstring(String s) {
 3         String vow = "aeiou";
 4         int res = 0;
 5         Map<Integer, Integer> hm = new HashMap<>();
 6         hm.put(0, -1);
 7         int mask = 0;
 8         int n = s.length();
 9         for(int i = 0; i < n; i++){
10             char c = s.charAt(i);
11             int ind = vow.indexOf(c);
12             mask ^= 1 << (ind + 1) >> 1;
13             hm.putIfAbsent(mask, i);
14             res = Math.max(res, i - hm.get(mask));
15         }
16 
17         return res;
18     }
19 }

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/69188.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

课上测试:位运算(AI)

2.使用位运算编写并调用下面函数,把当前时间(使用C库函数获得)设置到TIME中,给出代码,使用git记录过程。为了使用位运算将当前时间设置到一个自定义的 TIME 结构体或变量中(尽管通常我们不会直接用位运算来处理时间,因为时间通常是由多个独立的字段如小时、分钟、秒等组…

vue3 pinia 存数据

pinia是vue2中的vuex,状态管理,可以实现数据共享 1、先安装 npm install pinia2、在main.ts中进行创建和载入3、在src下新建store文件夹 定义存的文件 4、在组件中使用 此时控制台会有具体的值。

笑傲江湖单机版安装教程+虚拟机一键端+GM+10职业单人副本

今天给大家带来一款单机游戏的架设:笑傲江湖10职业单机版单人副本坐骑门徒新时装商完整任务。 另外:本人承接各种游戏架设(单机+联网) 本人为了学习和研究软件内含的设计思想和原理,带了架设教程仅供娱乐。 教程是本人亲自搭建成功的,绝对是完整可运行的,踩过的坑都给你…

Eplan插件 - 自由文本编辑器

前言 使用此插件可以快速完成对项目中的自由文本、路径功能文本的修改、删除等操作。 插件介绍 用户界面 插件UI界面进行了更新,相比较之前的插件界面风格更清爽简洁。功能介绍插件批量将选中文本中的源文本替换为修改文本。 插件支持多种选择方式,可以在绘图区选中文本,也可…

Logisim-015-偶校验检错

仓库地址 https://gitee.com/gitliang/logisim-to-cpu

Android开发:日志功能备忘

临时记一下吧,以后就直接复制粘贴这里面的好了。实现一个日志记录程序的运行状态,并且带上时间信息,可以写一个类灵活调用。 MyLog.java package com.example.networkaccessrestrictions;import static android.content.ContentValues.TAG;import android.content.Context; …

学年(2024-2025-3) 学号(20241424)《计算机基础与程序设计》第三周学习总结

学期(2024-2025-3) 学号(20241424) 《计算机基础与程序设计》第三周学习总结 作业信息 |这个作业属于([2024-2025-3-计算机基础与程序设计](https://www.cnblogs.com/rocedu/p/9577842.html#WEEK03)| |-- |-- | |这个作业要求在(2024-2025-3计算机基础与程序设计第三周作…