【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,next[0] = -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 双函数,以后任何「单模式串匹配」困难题直接秒。

相关推荐
lang201509282 分钟前
Java WebSocket API:JSR-356详解
java·python·websocket
晓13133 分钟前
第二章 【C语言篇:入门】 C 语言基础入门
c语言·算法
jiang_changsheng4 分钟前
环境管理工具全景图与深度对比
java·c语言·开发语言·c++·python·r语言
计算机学姐7 分钟前
基于SpringBoot的民宿预定管理系统【三角色+个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·mysql·信息可视化·intellij-idea·推荐算法
yaoxin52112310 分钟前
314. Java Stream API - 使用 Collectors.partitioningBy() 分区元素
java·windows
yong999014 分钟前
MATLAB面波频散曲线反演程序
开发语言·算法·matlab
LeoZY_18 分钟前
开源项目精选:Dear ImGui —— 轻量高效的 C++ 即时模式 GUI 框架
开发语言·c++·ui·开源·开源软件
Fightting8819 分钟前
Tkinter Button bind hover message
开发语言·python
noBt26 分钟前
Windows IDEA 卡顿严重
java·ide·intellij-idea
JicasdC123asd28 分钟前
【工业检测】基于YOLO13-C3k2-EIEM的铸造缺陷检测与分类系统_1
人工智能·算法·分类