zoukankan      html  css  js  c++  java
  • LeetCode 每日一题 1371. 每个元音包含偶数次的最长子字符串

    给你一个字符串 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 只包含小写英文字母。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/find-the-longest-substring-containing-vowels-in-even-counts
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


    要求每个元音均为偶数次,显然,用异或状压。

    class Solution {
     public:
      int findTheLongestSubstring(string s) {
        const string vowels("aeiou");
        vector<vector<int>>g(1 << 5);
        int t = 0;
        g[0].push_back(0);
        for(int i = 0, n = s.length(); i < n; ++i) {
          for(int j = 0; j < vowels.length(); ++j) {
            if(s[i] == vowels[j])
              t ^= 1 << j;
          }
          if(g[t].size() < 2)
            g[t].push_back(i + 1);
          else
            g[t][1] = i + 1;
        }
        int ans(0);
        for(int i = 0; i < (1 << 5); ++i) {
          if(g[i].size() > 1)
            ans = max(ans, g[i].back() - g[i][0]);
        }
        return ans;
      }
    };
    
  • 相关阅读:
    C艹函数与结构体
    c++ const 用法总结
    c++ 重载
    c++ 的makefile文件实例
    python3 异步模块asyncio
    C++ 面向对象 类成员函数this指针
    基于注释的Spring Security实战
    web 安全 初探 (正在更新)
    Spring dbcp连接池简单配置 示例
    Spring JDBC
  • 原文地址:https://www.cnblogs.com/Forgenvueory/p/12921631.html
Copyright © 2011-2022 走看看