JavaInterview JavaInterview
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)

『Java面试+Java学习』
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)
  • 指南
  • 简历

  • Java

  • 面试

  • 算法

  • algorithm
  • leetcode
JavaInterview.cn
2024-09-28
目录

每个元音包含偶数次的最长子字符串Java

文章发布较早,内容可能过时,阅读注意甄别。

# 题目

给你一个字符串 s ,请你返回满足以下条件的最长子字符串的长度:每个元音字母,即 'a','e','i','o','u' ,在子字符串中都恰好出现了偶数次。

示例 1:

输入:s = "eleetminicoworoep"
输出:13
解释:最长子字符串是 "leetminicowor" ,它包含 e,i,o 各 2 个,以及 0 个 a,u 。

示例 2:

输入:s = "leetcodeisgreat"
输出:5
解释:最长子字符串是 "leetc" ,其中包含 2 个 e 。

示例 3:

输入:s = "bcbcbc"
输出:6
解释:这个示例中,字符串 "bcbcbc" 本身就是最长的,因为所有的元音 a,e,i,o,u 都出现了 0 次。

提示:

  • 1 <= s.length <= 5 x 10^5
  • s 只包含小写英文字母。

# 思路

HashMap<Character,Integer> hashMap = new HashMap<Character, Integer>();

# 解法


class Solution {
    public int findTheLongestSubstring(String s) {
        HashMap<Character,Integer> hashMap = new HashMap<Character, Integer>();
        HashMap<Integer,Integer> stateMap = new HashMap<Integer, Integer>();
        hashMap.put('a',1);
        hashMap.put('i',2);
        hashMap.put('u',4);
        hashMap.put('e',8);
        hashMap.put('o',16);
        int res = 0;
        int max = 0;
        stateMap.putIfAbsent(0, -1);
        for(int i = 0;i<s.length();i++){
            if (hashMap.containsKey(s.charAt(i))) {
                res ^= hashMap.get(s.charAt(i));
            }
            if (stateMap.containsKey(res)) max = Math.max(max,i-stateMap.get(res));
            stateMap.putIfAbsent(res,i);
        }
        return max;
    }
}```

## 总结

- 分析出几种情况,然后分别对各个情况实现 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
微信 支付宝
#Java
最近更新
01
1686. 石子游戏VI Java
08-18
02
1688. 比赛中的配对次数 Java
08-18
03
1687. 从仓库到码头运输箱子 Java
08-18
更多文章>
Theme by Vdoing | Copyright © 2019-2025 JavaInterview.cn
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式