LeetCode 3302.字典序最小的合法序列

给你两个字符串 word1 和 word2 。

如果一个字符串 x 修改 至多 一个字符会变成 y ,那么我们称它与 y 几乎相等 。

如果一个下标序列 seq 满足以下条件,我们称它是 合法的 :

下标序列是 升序 的。

将 word1 中这些下标对应的字符 按顺序 连接,得到一个与 word2 几乎相等 的字符串。

请你返回一个长度为 word2.length 的数组,表示一个 字典序最小 的 合法 下标序列。如果不存在这样的序列,请你返回一个 空 数组。

注意 ,答案数组必须是字典序最小的下标数组,而 不是 由这些下标连接形成的字符串。

示例 1:

输入:word1 = "vbcca", word2 = "abc"

输出:[0,1,2]

解释:

字典序最小的合法下标序列为 [0, 1, 2] :

将 word1[0] 变为 'a' 。

word1[1] 已经是 'b' 。

word1[2] 已经是 'c' 。

示例 2:

输入:word1 = "bacdc", word2 = "abc"

输出:[1,2,4]

解释:

字典序最小的合法下标序列为 [1, 2, 4] :

word1[1] 已经是 'a' 。

将 word1[2] 变为 'b' 。

word1[4] 已经是 'c' 。

示例 3:

输入:word1 = "aaaaaa", word2 = "aaabc"

输出:[]

解释:

没有合法的下标序列。

示例 4:

输入:word1 = "abc", word2 = "ab"

输出:[0,1]

提示:

1 <= word2.length < word1.length <= 3 * 105^55

word1 和 word2 只包含小写英文字母。

我们需要找到下标的字典序最小的合法序列,因此在对word1和word2进行匹配时,如果两个字符相等,那么一定要匹配上,这样才能保证下标的字典序最小;否则两个字符不等时,如果把当前word1中的字符修改为word2中的字符后,后面如果两个字符串还能匹配上,则需要把当前字符加入答案数组,如何知道后面两个字符串是否还能匹配上,可以使用后缀数组,我们先反向遍历word1和word2,找出word1的后缀能匹配到的最长word2后缀,然后用数组把每个word1下标对应的能匹配到的最长word2后缀的开始位置记录下来:

cpp 复制代码
class Solution {
public:
    vector<int> validSequence(string word1, string word2) {
        int size1 = word1.size();
        int size2 = word2.size();

        vector<int> suf(size1 + 1);
        suf[size1] = size2;

        int idx1 = size1 - 1;
        int idx2 = size2 - 1;

        while (idx1 >= 0 && idx2 >= 0) {
            if (word1[idx1] == word2[idx2]) {
                --idx2;
            }

            // word1的idx1下标最多能匹配到word2[idx2 + 1, word2.size()]
            suf[idx1] = idx2 + 1;
            --idx1;
        }

        while (idx1 >= 0) {
            suf[idx1] = idx2 + 1;
            --idx1;
        }

        vector<int> ans(size2);

        idx1 = 0;
        idx2 = 0;

        bool hasChanged = false;

        while (idx1 < size1 && idx2 < size2) {
            // 如果能匹配到
            // 或没有改变过,且改变后可以匹配成功
            // 则将idx1加入结果数组
            if (word1[idx1] == word2[idx2] || 
                !hasChanged && suf[idx1 + 1] <= idx2 + 1) {
   
                if (word1[idx1] != word2[idx2]) {
                    hasChanged = true;
                }

                ans[idx2] = idx1;

                ++idx1;
                ++idx2;
            } else {
                ++idx1;
            }

            // 找到目标长度的答案
            if (idx2 == size2) {
                return ans;
            }
        }

        return {};
    }
};

如果word1的长度为n,则此算法时间复杂度为O(n),空间复杂度为O(n)。

相关推荐
YoungHong199212 分钟前
面试经典150题[072]:从前序与中序遍历序列构造二叉树(LeetCode 105)
leetcode·面试·职场和发展
im_AMBER1 小时前
Leetcode 78 识别数组中的最大异常值 | 镜像对之间最小绝对距离
笔记·学习·算法·leetcode
LYFlied2 小时前
【每日算法】LeetCode 25. K 个一组翻转链表
算法·leetcode·链表
LYFlied5 小时前
【每日算法】LeetCode 19. 删除链表的倒数第 N 个结点
算法·leetcode·链表
努力学算法的蒟蒻6 小时前
day35(12.16)——leetcode面试经典150
算法·leetcode·面试
LYFlied7 小时前
【每日算法】LeetCode 234. 回文链表详解
算法·leetcode·链表
刃神太酷啦7 小时前
C++ list 容器全解析:从构造到模拟实现的深度探索----《Hello C++ Wrold!》(16)--(C/C++)
java·c语言·c++·qt·算法·leetcode·list
承渊政道7 小时前
一文彻底搞清楚链表算法实战大揭秘和双向链表实现
c语言·数据结构·算法·leetcode·链表·visual studio
玉树临风ives9 小时前
atcoder ABC436 题解
c++·算法·leetcode·atcoder·信息学奥赛
圣保罗的大教堂9 小时前
leetcode 2110. 股票平滑下跌阶段的数目 中等
leetcode