【594 · 字符串查找 II】

594 · 字符串查找 II

困难 | 30% 通过率 | 要求 O(n + m)

→ 标准「KMP 模板题」。

继续用「快刷四部曲」5 分钟背完、一辈子会用。


第一步:识别模式

  • 题型:单模式串精确匹配
  • 模式:KMP(Knuth--Morris--Pratt)
  • 关键 :预处理 next[] 数组,失配时 不回退主串指针
  • 复杂度 :预处理 O(m) + 匹配 O(n) = O(n + m),空间 O(m)

第二步:选择模板

KMP 双函数固定写法:

  1. buildNext(String p) → 返回 next[]
  2. strStr(String s, String p) → 返回首位置或 -1

第三步:注意边界

  • 空模式串:约定返回 0(与 Java 内置 indexOf 一致)
  • 主串长度 n、模式串长度 m,m > n 时直接 -1
  • next[] 长度 = m,next0 = -1(经典 -1 写法最简洁)

第四步:成功秒杀(完整代码)

java 复制代码
public class Solution {
    /**
     * @param source: the source string
     * @param target: the target string
     * @return: the first index of target in source, or -1 if not exist
     */
    public int strStr(String source, String target) {
        if (target == null || source == null) return -1;
        int n = source.length(), m = target.length();
        if (m == 0) return 0;          // 空模式串约定返回 0
        if (m > n) return -1;          // 剪枝

        int[] next = buildNext(target);
        int i = 0, j = 0;              // i 指向 source,j 指向 target
        while (i < n) {
            if (j == -1 || source.charAt(i) == target.charAt(j)) {
                i++; j++;
            } else {
                j = next[j];           // 失配,j 回退
            }
            if (j == m) {              // 完全匹配
                return i - j;
            }
        }
        return -1;
    }

    // 预处理 next 数组,next[0] = -1
    private int[] buildNext(String p) {
        int m = p.length();
        int[] next = new int[m];
        next[0] = -1;
        int i = 0, j = -1;
        while (i < m - 1) {
            if (j == -1 || p.charAt(i) == p.charAt(j)) {
                i++; j++;
                next[i] = j;
            } else {
                j = next[j];
            }
        }
        return next;
    }
}

复杂度

  • 时间:O(n + m)
  • 空间 :O(m) 仅 next[]

背下 buildNext + strStr 双函数,以后任何「单模式串匹配」困难题直接秒。

相关推荐
wind100328 分钟前
Qt5Widgets.dll 缺失报错|游戏软件无法启动完整修复教程
开发语言·qt·游戏·电脑
我叫张土豆18 分钟前
在 OpenClaw Java 对话框输入一句话后,系统是如何运行的?
java·开发语言·人工智能
liangbo725 分钟前
JVM规范第 4 章:class 文件格式
java·jvm
橘子汽水16826 分钟前
Leetcode 236,437:二叉树的最近公共祖先,路径总和III
java·数据结构
牛油果子哥q27 分钟前
C++模板万字深度精讲:函数/类模板特化、全特化与偏特化、SFINAE机制、类型萃取、模板元编程入门、工程实战落地
开发语言·c++·面试
数据狐(Datafox)33 分钟前
1688商品详情API技术解析与落地应用(含标准 JSON 示例)
java·数据库·json
M78佐菲35 分钟前
Linux学习笔记:进程回收、exec函数簇与线程
linux·笔记·学习·算法
linux-hzh37 分钟前
百日算法修炼 · Day 18
算法
用户8132679332539 分钟前
Python 计算盘中 VWAP:为什么 1 分钟 K 线是量化工程中的高性价比选择
后端·算法·github
神仙别闹41 分钟前
基于C++ 实现 L 系统分形树
开发语言·c++