Leetcode1839. 所有元音按顺序排布的最长子字符串

Every day a Leetcode

题目来源:1839. 所有元音按顺序排布的最长子字符串

解法1:滑动窗口

要找的是最长美丽子字符串的长度,我们可以用滑动窗口解决。

设窗口内的子字符串为 window,每当 wordright >= window.back() 时,说明可以按字典序升序排布,我们都可以将 wordright 添加进 window 的末尾,我们使用一个集合 cnt 存储每次遇到的 wordright,当 cnt.size() = 5时,说明所有 5 个英文元音字母都至少出现了一次,更新长度:max_len = max(max_len, (int)window.size())。

如果遇到了不按字典序升序排布的情况,我们发现窗口必须全部清空,于是有:window = "",cnt.clear(),同时要改变窗口的左边界 left = right,立即将 wordleft 加入进窗口和集合,重新开始遍历。

代码:

c 复制代码
/*
 * @lc app=leetcode.cn id=1839 lang=cpp
 *
 * [1839] 所有元音按顺序排布的最长子字符串
 */

// @lc code=start

// 滑动窗口

class Solution
{
public:
    int longestBeautifulSubstring(string word)
    {
        // 特判
        if (word.size() < 5)
            return 0;
        int n = word.size();
        string window;
        unordered_set<char> cnt;
        int max_len = 0, left = 0, right = 0;
        while (right < n)
        {
            if (window.empty() || word[right] >= window.back())
            {
                window += word[right];
                cnt.insert(word[right]);
                right++;
                if (cnt.size() == 5)
                    max_len = max(max_len, (int)window.size());
            }
            else
            {
                window = "";
                cnt.clear();
                left = right;
                window += word[left];
                cnt.insert(word[left]);
                right++;
            }
        }
        return max_len;
    }
};
// @lc code=end

结果:

复杂度分析:

时间复杂度:O(n),其中 n 是字符串 word 的长度。

空间复杂度:O(n),其中 n 是字符串 word 的长度。

解法2:一次遍历

由滑动窗口的启发,我们发现只设立几个变量也能达到相同的效果。

代码:

c 复制代码
class Solution
{
public:
    int longestBeautifulSubstring(string word)
    {
        // 特判
        if (word.size() < 5)
            return 0;
        int n = word.size();
        int max_len = 0;
        int vowel = 1, cur_len = 1;
        for (int i = 1; i < n; i++)
        {
            if (word[i] >= word[i - 1])
            {
                cur_len++;
                if (word[i] > word[i - 1])
                    vowel++;
            }
            else
            {
                cur_len = 1;
                vowel = 1;
            }
            if (vowel == 5)
                max_len = max(max_len, cur_len);
        }
        return max_len;
    }
};

结果:

复杂度分析:

时间复杂度:O(n),其中 n 是字符串 word 的长度。

空间复杂度:O(1)。

相关推荐
csdn_aspnet34 分钟前
C++ 未排序数组中第 k 个最小/最大元素 | 最坏情况下的线性时间
数据结构·c++·算法
phltxy10 小时前
C语言操作符详解
java·c语言·算法
aqiu11111110 小时前
【LeetCode 902】最大为 N 的数字组合 - 详细题解与数位组合思路
数据结构·算法·leetcode·蓝桥杯·数位dp
辰烨chenye11 小时前
LeetCode Hot 100 题解 · 哈希篇
算法·leetcode·哈希算法
罗西的思考11 小时前
DreamZero 与 DreamDojo:世界模型与策略的分层协同综合分析与对比
人工智能·算法·机器学习
玖玥拾12 小时前
LeetCode 219 存在重复元素 II
算法·leetcode·哈希算法·散列表
知无不研13 小时前
c语言中循环的介绍与简单应用
c语言·开发语言·算法·循环·for·while
a1879272183114 小时前
【算法】动态规划第四篇:背包收官——min 哨兵、计数世界与组合排列分水岭
算法·leetcode·动态规划·dp·01背包·算法讲解·决策合并
2601_9622974814 小时前
在python3中、下列输出变量a的正确写法是_2020超星大数据Python免费答案
数据结构·python·算法·编程·字符串操作