首页 > 其他分享 >LeetCode 1371. Find the Longest Substring Containing Vowels in Even Counts

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

时间:2024-10-08 22:44:15浏览次数:1  
标签:Even even Containing int mask vowels Substring longest need

原题链接在这里: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 }

 

标签:Even,even,Containing,int,mask,vowels,Substring,longest,need
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18453207

相关文章

  • 对UVM添加超时前的打印信息+AXI低功耗接口+process的await语句+对象当成参数+sv的单例
    对UVM添加超时前的打印信息首先获取到UVM的超时值,然后手动设定\$time的比较和while延时循环,当超出时间后,打印特殊的debug信息。$time<set_time,则进行循环。uvm_cmdline_processorclp;clp=uvm_cmdline_processor::get_inst();stringtimeout_settings[$];stringtimeout......
  • SOMEIP_ETS_164: SD_SubscribeEventgroup_with_unallowed_option_ip_2
    测试目的:验证DUT能够拒绝一个在请求中包含错误参数(端点选项中包含无效IPv4地址,即111.111.111.111)的SubscribeEventgroup消息,并以SubscribeEventgroupNAck作为响应。描述本测试用例旨在确保DUT遵循SOME/IP协议,当接收到一个在端点选项中包含无效IPv4地址(111.111.111.111)的S......
  • 5782 Special Event 结构体排序
    解决思路 初始化:定义一个结构体 node 来存储每一天的可用人数和天的编号。 读取输入:读取每个人的可用性,并统计每一天的可用人数。 排序:根据每一天的可用人数进行排序。 输出结果:输出可用人数最多的天的编号,如果有多天满足条件,用逗号分隔。#include<bits/s......
  • wait_event_interruptible_timeout() 函数
     原文链接:https://blog.csdn.net/wuyongpeng0912/article/details/45723657 网上有关于此函数的分析,但大都是同一篇文章转载来转载去,没有进一步的分析。做个小结:了解函数功能,除了直接看代码逻辑,最有效的当是注释内容了。如下:函数原型:wait_event_interruptible_timeout......
  • C# 事件(Event)应用说明一
    一.C# 事件(Event)定义说明:C#事件(Event)是一种成员,用于将特定的事件通知发送给订阅者。事件通常用于实现观察者模式,它允许一个对象将状态的变化通知给其他对象,而不需要知道这些对象的具体细节。事件(Event) 基本上说是一个用户操作,或者是一些提示信息,如系统生成的通知、按键输......
  • C# 事件(Event)应用说明二
    简单示例如下一.界面显示: 二.源代码示例: //定义一个委托类型,用于事件处理程序publicdelegatevoidMyEventHandler(objectsender,EventArgse);//发布者类publicclassProcessBusinessClass{//声明事......
  • Event和Activity
    在JAINSLEE中,Event(事件)和Activity(活动)是两个核心概念,它们共同作用于系统的执行过程,但它们代表不同的含义和职责。让我们从最基础的层面来讲解它们的区别、联系,以及它们在JAINSLEE框架中的角色。1.Event(事件)1.1概念事件(Event)是JAINSLEE中的一个基本单元,用来......
  • 程序埋点(Event Tracking)
    程序埋点(EventTracking)是指在软件程序中嵌入记录用户行为或系统运行状态的代码,以便收集数据用于分析和监控。程序埋点通常用于分析用户行为、性能监控、问题排查、产品优化等目的。何时使用程序埋点:用户行为分析:了解用户在应用中的行为,例如点击量、页面访问次数等。性能监控:......
  • 易优CMS致命错误,联系技术支持:Call to undefined function eyPreventShell()-eyoucms
    当你遇到 core/helper.php 第146行左右出现致命错误,并且提示 CalltoundefinedfunctioneyPreventShell() 时,通常是因为某个自定义函数未被定义或未被正确引入。以下是一些具体的解决步骤:步骤1:检查函数定义定位 eyPreventShell 函数查找 eyPreventShell 函数的......
  • 【VUE】[Violation] Added non-passive event listener to a scroll-blocking...
    1.问题[Violation]Addednon-passiveeventlistenertoascroll-blocking<某些>事件.Considermarkingeventhandleras'passive'tomakethepagemoreresponsive.See<URL>译:[违规]向滚动阻止添加了非被动事件侦听器<某些>事件.请考虑将事件处理程序标记为“被......