【LeetCode 刷题】字符串-字符串匹配(KMP)

此博客为《代码随想录》字符串章节的学习笔记,主要内容为字符串匹配 KMP 算法相关的题目解析。

文章目录

  • [28. 找出字符串中第一个匹配项的下标](#28. 找出字符串中第一个匹配项的下标)
  • [459. 重复的子字符串](#459. 重复的子字符串)

28. 找出字符串中第一个匹配项的下标

题目链接

python 复制代码
class Solution:
    def getNext(self, s: str) -> List[int]:
        j, nxt = 0, [0] * len(s)
        for i in range(1, len(s)):
            while j > 0 and s[i] != s[j]:
                j = nxt[j - 1]
            if s[i] == s[j]:
                j += 1
            nxt[i] = j
        return nxt
        
    def strStr(self, haystack: str, needle: str) -> int:
        j, nxt = 0, self.getNext(needle)
        for i in range(len(haystack)):
            while j > 0 and haystack[i] != needle[j]:
                j = nxt[j - 1]
            if haystack[i] == needle[j]:
                j += 1
            if j == len(needle):
                return i - j + 1
        return -1
  • getNext 用于计算 next 数组,next 数组是模式串的特征规律,与文本串无关
  • 在此代码中的 next 数组的定义下,每次遇到矛盾,都需要查找上一个元素next 数组中的值,以确定回溯位置
  • getNext 函数中 j 表示的是目前最长公共前后缀的长度(也指向着公共前缀的末尾);i 指向着公共后缀的末尾
  • getNext 的 for 循环:i 从 1 开始 ,因为 i 为 0 时表示着长度为 1 的串,最长公共前后缀长度为 0,即 next[0] = 0(在数组初始化时已经完成)
  • strStr 的 for 循环:i 从 0 开始 ,完整遍历文本串 haystack

459. 重复的子字符串

题目链接

python 复制代码
class Solution:
    def getNext(self, s: str) -> List[int]:
        j, nxt = 0, [0] * len(s)
        for i in range(1, len(s)):
            while j > 0 and s[i] != s[j]:
                j = nxt[j - 1]
            if s[i] == s[j]:
                j += 1
            nxt[i] = j
        return nxt

    def repeatedSubstringPattern(self, s: str) -> bool:
        nxt = self.getNext(s)
        if nxt[-1] != 0 and len(s) % (len(s) - nxt[-1]) == 0:
            return True
        return False
  • 如果字符串 s 是由重复子串组成,那么最长相等前后缀不包含的子串 一定是 s 的最小重复子串,证明过程
  • 因此仿照 KMP 算法计算 next 数组,判断是否存在最长相等前后缀,以及最长相等前后缀不包含的子串的长度 len(s) - nxt[-1] ,是否能够被原字符串长度整除

移动匹配法(通用做法)

python 复制代码
class Solution:
    def repeatedSubstringPattern(self, s: str) -> bool:
        s_ = (s + s)[1:-1]
        return s in s_
  • 自身加自身得到 s_,之后移除第一和最后一个元素(破坏原始串),判断 s_ 中是否包含自身 s
相关推荐
农夫山泉2号10 分钟前
【python】—conda新建python3.11的环境报错
python·conda·python3.11
AndrewHZ1 小时前
【图像处理基石】什么是油画感?
图像处理·人工智能·算法·图像压缩·视频处理·超分辨率·去噪算法
.格子衫.1 小时前
015枚举之滑动窗口——算法备赛
数据结构·算法
ZHOU_WUYI1 小时前
Flask Docker Demo 项目指南
python·docker·flask
J先生x2 小时前
【IP101】图像处理进阶:从直方图均衡化到伽马变换,全面掌握图像增强技术
图像处理·人工智能·学习·算法·计算机视觉
爱coding的橙子5 小时前
每日算法刷题 Day3 5.11:leetcode数组2道题,用时1h(有点慢)
算法·leetcode
码上淘金6 小时前
【Python】Python常用控制结构详解:条件判断、遍历与循环控制
开发语言·python
Brilliant Nemo6 小时前
四、SpringMVC实战:构建高效表述层框架
开发语言·python
2301_787552876 小时前
console-chat-gpt开源程序是用于 AI Chat API 的 Python CLI
人工智能·python·gpt·开源·自动化
懵逼的小黑子6 小时前
Django 项目的 models 目录中,__init__.py 文件的作用
后端·python·django