当前位置: 首页 > article >正文

Leetcode 3302. Find the Lexicographically Smallest Valid Sequence

  • Leetcode 3302. Find the Lexicographically Smallest Valid Sequence
    • 1. 解题思路
    • 2. 代码实现
  • 题目链接:3302. Find the Lexicographically Smallest Valid Sequence

1. 解题思路

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

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

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

2. 代码实现

给出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。


http://www.kler.cn/a/325553.html

相关文章:

  • Anaconda虚拟环境默认路径在C盘怎么更改
  • 【bash】将本地未合入 master 的分支,生成对应 patche 文件
  • 计算机毕业设计 办公用品管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • JavaScript中的函数定义
  • 位图:如何实现网页爬虫中的 URL 去重功能?
  • 网络通信(学习笔记)
  • 【重学 MySQL】四十二、单行子查询
  • 城市大脑:智慧城市的神经中枢——典型实践与经验启示
  • K8s安装部署(v1.28)--超详细(cri-docker作为运行时)
  • Spring Boot 3.x 配置 Spring Doc以及导入postman带图详解
  • 数据集-目标检测系列-鼠检测数据集 mouse >> DataBall
  • 自动蛋鸡饲料机组:粉碎搅拌一步到位
  • 【高频SQL基础50题】11-15
  • Linux中的tr命令详解
  • 11-pg内核之锁管理器(六)死锁检测
  • PostgreSQL 一张表多个字段关联另一张表
  • 路由器的天线有什么用?数量多≠信号强?
  • C++番外篇-------排序算法总结
  • 数字孪生平台,助力制造设备迈入超感知与智控新时代!
  • 《C++多态性:开启实际项目高效编程之门》