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

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 只包含小写英文字母。

我的代码

class Solution {
     
public:
    int longestSubstring(const string &s, int start, int end, map<char, int> &count) {
     
        if (count['a'] % 2) {
     
            auto temp = count;
            bool isFind = false;
            int len1 = 0, len2 = 0;
            for (int i = start; i <= end; ++i) {
     
                switch (s[i]) {
     
                    case 'a': --count['a'], isFind = true; break;
                    case 'e': --count['e']; break;
                    case 'i': --count['i']; break;
                    case 'o': --count['o']; break;
                    case 'u': --count['u']; break;
                    default: break;
                }
                if (isFind) {
     
                    len1 = longestSubstring(s, i + 1, end, count);
                    break;
                }
            }
            isFind = false;
            count = temp;
            for (int i = end; i >= start; --i) {
     
                switch (s[i]) {
     
                    case 'a': --count['a'], isFind = true; break;
                    case 'e': --count['e']; break;
                    case 'i': --count['i']; break;
                    case 'o': --count['o']; break;
                    case 'u': --count['u']; break;
                    default: break;
                }
                if (isFind) {
     
                    len2 = longestSubstring(s, start, i - 1, count);
                    break;
                }
            }
            return max(len1, len2);
        } else if (count['e'] % 2) {
     
            auto temp = count;
            bool isFind = false;
            int len1 = 0, len2 = 0;
            for (int i = start; i <= end; ++i) {
     
                switch (s[i]) {
     
                    case 'a': --count['a']; break;
                    case 'e': --count['e'], isFind = true; break;
                    case 'i': --count['i']; break;
                    case 'o': --count['o']; break;
                    case 'u': --count['u']; break;
                    default: break;
                }
                if (isFind) {
     
                    len1 = longestSubstring(s, i + 1, end, count);
                    break;
                }
            }
            isFind = false;
            count = temp;
            for (int i = end; i >= start; --i) {
     
                switch (s[i]) {
     
                    case 'a': --count['a']; break;
                    case 'e': --count['e'], isFind = true; break;
                    case 'i': --count['i']; break;
                    case 'o': --count['o']; break;
                    case 'u': --count['u']; break;
                    default: break;
                }
                if (isFind) {
     
                    len2 = longestSubstring(s, start, i - 1, count);
                    break;
                }
            }
            return max(len1, len2);
        } else if (count['i'] % 2) {
     
            auto temp = count;
            bool isFind = false;
            int len1 = 0, len2 = 0;
            for (int i = start; i <= end; ++i) {
     
                switch (s[i]) {
     
                    case 'a': --count['a']; break;
                    case 'e': --count['e']; break;
                    case 'i': --count['i'], isFind = true; break;
                    case 'o': --count['o']; break;
                    case 'u': --count['u']; break;
                    default: break;
                }
                if (isFind) {
     
                    len1 = longestSubstring(s, i + 1, end, count);
                    break;
                }
            }
            isFind = false;
            count = temp;
            for (int i = end; i >= start; --i) {
     
                switch (s[i]) {
     
                    case 'a': --count['a']; break;
                    case 'e': --count['e']; break;
                    case 'i': --count['i'], isFind = true; break;
                    case 'o': --count['o']; break;
                    case 'u': --count['u']; break;
                    default: break;
                }
                if (isFind) {
     
                    len2 = longestSubstring(s, start, i - 1, count);
                    break;
                }
            }
            return max(len1, len2);
        } else if (count['o'] % 2) {
     
            auto temp = count;
            bool isFind = false;
            int len1 = 0, len2 = 0;
            for (int i = start; i <= end; ++i) {
     
                switch (s[i]) {
     
                    case 'a': --count['a']; break;
                    case 'e': --count['e']; break;
                    case 'i': --count['i']; break;
                    case 'o': --count['o'], isFind = true; break;
                    case 'u': --count['u']; break;
                    default: break;
                }
                if (isFind) {
     
                    len1 = longestSubstring(s, i + 1, end, count);
                    break;
                }
            }
            isFind = false;
            count = temp;
            for (int i = end; i >= start; --i) {
     
                switch (s[i]) {
     
                    case 'a': --count['a']; break;
                    case 'e': --count['e']; break;
                    case 'i': --count['i']; break;
                    case 'o': --count['o'], isFind = true; break;
                    case 'u': --count['u']; break;
                    default: break;
                }
                if (isFind) {
     
                    len2 = longestSubstring(s, start, i - 1, count);
                    break;
                }
            }
            return max(len1, len2);
        } else if (count['u'] % 2) {
     
            auto temp = count;
            bool isFind = false;
            int len1 = 0, len2 = 0;
            for (int i = start; i <= end; ++i) {
     
                switch (s[i]) {
     
                    case 'a': --count['a']; break;
                    case 'e': --count['e']; break;
                    case 'i': --count['i']; break;
                    case 'o': --count['o']; break;
                    case 'u': --count['u'], isFind = true; break;
                    default: break;
                }
                if (isFind) {
     
                    len1 = longestSubstring(s, i + 1, end, count);
                    break;
                }
            }
            isFind = false;
            count = temp;
            for (int i = end; i >= start; --i) {
     
                switch (s[i]) {
     
                    case 'a': --count['a']; break;
                    case 'e': --count['e']; break;
                    case 'i': --count['i']; break;
                    case 'o': --count['o']; break;
                    case 'u': --count['u'], isFind = true; break;
                    default: break;
                }
                if (isFind) {
     
                    len2 = longestSubstring(s, start, i - 1, count);
                    break;
                }
            }
            return max(len1, len2);
        } else {
     
            return end + 1 - start;
        }
    }

    int findTheLongestSubstring(string s) {
     
        int len = s.size();
        map<char, int> count;
        count['a'] = 0, count['e'] = 0, count['i'] = 0, count['o'] = 0, count['u'] = 0;
        for (const auto &c: s) {
     
            switch (c) {
     
                case 'a': ++count['a']; break;
                case 'e': ++count['e']; break;
                case 'i': ++count['i']; break;
                case 'o': ++count['o']; break;
                case 'u': ++count['u']; break;
                default: break;
            }
        }
        return longestSubstring(s, 0, len - 1, count);
    }
};

我的成绩

执行结果:超出时间限制。

一些想法

本道题我使用了递归,单纯的递归果然是无法解决中等难度的题的。。。

执行用时为 32 ms 的范例

#include 
#include 
#include 
#include 

using namespace std;

static auto fast = [] {
     
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    return 0;
}();

class Solution {
     
  public:
    int findTheLongestSubstring(string s) {
     
        vector<int> sts(32, -1);
        sts[0] = 0;
        int j = 0;
        int ans = 0;
        for(int i = 0; i < s.size(); i++) {
     
            if(s[i] == 'a')
                j ^= 1;
            else if(s[i] == 'i')
                j ^= 2;
            else if(s[i] == 'u')
                j ^= 4;
            else if(s[i] == 'e')
                j ^= 8;
            else if(s[i] == 'o')
                j ^= 16;
            if(sts[j] == -1)
                sts[j] = i + 1;
            else
                ans = max(ans, i + 1 - sts[j]);
        }
        return ans;
    }
};

思考

不会做。。。有时间把范例看一下

你可能感兴趣的:(我的leetcode刷题系列,leetcode,字符串)