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

Every day a Leetcode

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

解法1:滑动窗口

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

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

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

代码:

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)。

相关推荐
threejs源码翻译官12 分钟前
显微镜图像处理【优化】- 使用图像风格迁移技术放大图像细节
算法
强德亨上校39 分钟前
贪心算法(Greedy Algorithm)详解
算法·贪心算法
浮灯Foden2 小时前
算法-每日一题(DAY13)两数之和
开发语言·数据结构·c++·算法·leetcode·面试·散列表
西工程小巴2 小时前
实践笔记-VSCode与IDE同步问题解决指南;程序总是进入中断服务程序。
c语言·算法·嵌入式
Tina学编程3 小时前
48Days-Day19 | ISBN号,kotori和迷宫,矩阵最长递增路径
java·算法
Moonbit3 小时前
MoonBit Perals Vol.06: MoonBit 与 LLVM 共舞 (上):编译前端实现
后端·算法·编程语言
执子手 吹散苍茫茫烟波3 小时前
leetcode415. 字符串相加
java·leetcode·字符串
百度Geek说4 小时前
第一!百度智能云领跑视觉大模型赛道
算法
big_eleven4 小时前
轻松掌握数据结构:二叉树
后端·算法·面试
big_eleven4 小时前
轻松掌握数据结构:二叉查找树
后端·算法·面试