首页 > 其他分享 >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浏览次数:14  
标签: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

相关文章

  • 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......
  • C# 事件(Event)应用说明一
    一.C# 事件(Event)定义说明:C#事件(Event)是一种成员,用于将特定的事件通知发送给订阅者。事件通常用于实现观察者模式,它允许一个对象将状态的变化通知给其他对象,而不需要知道这些对象的具体细节。事件(Event) 基本上说是一个用户操作,或者是一些提示信息,如系统生成的通知、按键输......
  • Event和Activity
    在JAINSLEE中,Event(事件)和Activity(活动)是两个核心概念,它们共同作用于系统的执行过程,但它们代表不同的含义和职责。让我们从最基础的层面来讲解它们的区别、联系,以及它们在JAINSLEE框架中的角色。1.Event(事件)1.1概念事件(Event)是JAINSLEE中的一个基本单元,用来......
  • 易优CMS致命错误,联系技术支持:Call to undefined function eyPreventShell()-eyoucms
    当你遇到 core/helper.php 第146行左右出现致命错误,并且提示 CalltoundefinedfunctioneyPreventShell() 时,通常是因为某个自定义函数未被定义或未被正确引入。以下是一些具体的解决步骤:步骤1:检查函数定义定位 eyPreventShell 函数查找 eyPreventShell 函数的......