Leetcode 3302. Find the Lexicographically Smallest Valid Sequence

  • [Leetcode 3302. Find the Lexicographically Smallest Valid Sequence](#Leetcode 3302. Find the Lexicographically Smallest Valid Sequence)
    • [1. 解题思路](#1. 解题思路)
    • [2. 代码实现](#2. 代码实现)

1. 解题思路

这一题的话由于至多只能够修改一个字符,因此,我们就是要考察每一个字符前正向的最大公共子序列的长度和其后方的从后往前的最大公共子序列的长度。如果两者相加不小于目标目标字符串word2的长度减一,即表示调整当前位置上的字符的话即可获得一个子串使之与目标字符串word2相同。

然后,我们定位到第一个满足上述条件的位置,通过调整该位置获得的字符串就是最优的选项。

最后,我们找到调整该位置所能够获得字符串的index即可。

2. 代码实现

给出python代码实现如下:

python 复制代码
class Solution:
    def validSequence(self, word1: str, word2: str) -> List[int]:
        n, m = len(word1), len(word2)
        if m == 1:
            return [0]
        
        # prefix
        prefix = [0 for _ in word1]
        i, j = 0, 0
        while j < m:
            while i < n and word1[i] != word2[j]:
                prefix[i] = j
                i += 1
            if i >= n:
                break
            j += 1
            prefix[i] = j
            i += 1
        while i < n:
            prefix[i] = j
            i += 1

        # suffix
        i, j = n-1, m-1
        suffix = [0 for _ in word1]
        while j >= 0:
            while i >= 0 and word1[i] != word2[j]:
                suffix[i] = m-1-j
                i -= 1
            if i < 0:
                break
            j -= 1
            suffix[i] = m-1-j
            i -= 1
        while i >= 0:
            suffix[i] = m-1-j
            i -= 1

        # find target idx
        idx = -1
        if word1[0] != word2[0] and suffix[1] >= m-1:
            idx = 0
        else:
            for i in range(1, n-1):
                if prefix[i-1] + suffix[i+1] >= m-1 and prefix[i] != prefix[i-1]+1:
                    idx = i
                    break
        if idx == -1 and prefix[n-2] >= m-1:
            idx = n-1

        # get all the index
        if idx == -1:
            return []
        i, j = 0, 0
        ans = []
        while j < m:
            while i < idx and word1[i] != word2[j]:
                i += 1
            if i >= idx:
                break
            ans.append(i)
            j += 1
            i += 1
        if j < m:
            ans.append(i)
            i += 1
            j += 1
        while j < m:
            while i < n and word1[i] != word2[j]:
                i += 1
            if i >= n:
                break
            ans.append(i)
            j += 1
            i += 1
        return ans

提交代码评测得到:耗时1200ms,占用内存55.7MB。

相关推荐
Espresso Macchiato1 个月前
Leetcode 3316. Find Maximum Removals From Source String
leetcode·动态规划·leetcode medium·leetcode 3316·leetcode双周赛141
Espresso Macchiato2 个月前
Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II
leetcode·滑动窗口·leetcode medium·leetcode 3306·leetcode周赛417
Espresso Macchiato2 个月前
Leetcode 3301. Maximize the Total Height of Unique Towers
leetcode·leetcode medium·leetcode双周赛140·leetcode 3301
摆烂小白敲代码2 个月前
【算法】最长公共子序列(C/C++)
c语言·数据结构·c++·算法·最长公共子序列·lcs
Espresso Macchiato3 个月前
Leetcode 3255. Find the Power of K-Size Subarrays II
leetcode·leetcode medium·leetcode 3255·leetcode 3254·leetcode周赛137
Espresso Macchiato4 个月前
Leetcode 3240. Minimum Number of Flips to Make Binary Grid Palindromic II
leetcode·leetcode medium·回文·leetcode 3240·leetcode双周赛136
Espresso Macchiato4 个月前
Leetcode 3234. Count the Number of Substrings With Dominant Ones
排列组合·leetcode medium·容斥原理·leetcode 3234·leetcode周赛408
Espresso Macchiato5 个月前
Leetcode 3201. Find the Maximum Length of Valid Subsequence I
leetcode medium·leetcode题解·leetcode 3201·leetcode周赛404
Espresso Macchiato5 个月前
Leetcode 3196. Maximize Total Cost of Alternating Subarrays
leetcode·动态规划·leetcode medium·leetcode周赛403·leetcode 3196