LeetCode 第28题:找出字符串中第一个匹配项的下标
1.LeetCode 第10题:正则表达式匹配2.LeetCode 第1题:两数之和3.LeetCode 第2题:两数相加4.LeetCode 第3题:无重复字符的最长子串5.LeetCode 第4题:寻找两个正序数组的中位数6.LeetCode 第8题:字符串转换整数 (atoi)7.LeetCode 第7题:整数反转8.LeetCode 第6题:Z字形变换9.LeetCode 第5题:最长回文子串10.LeetCode 第9题:回文数11.LeetCode 第11题:盛最多水的容器12.LeetCode 第12题:整数转罗马数字13.LeetCode 第13题:罗马数字转整数14.LeetCode 第14题:最长公共前缀15.LeetCode 第15题:三数之和16.LeetCode 第16题:最接近的三数之和17.LeetCode 第17题:电话号码的字母组合18.LeetCode 第18题:四数之和19.LeetCode 第19题:删除链表的倒数第N个结点20.LeetCode 第20题:有效的括号21.LeetCode 第21题:合并两个有序链表22.LeetCode 第22题:括号生成23.LeetCode 第23题:合并K个升序链表24.LeetCode 第24题:两两交换链表中的节点25.LeetCode 第25题:K 个一组翻转链表26.LeetCode 第26题:删除有序数组中的重复项27.LeetCode 第27题:移除元素
28.LeetCode 第28题:找出字符串中第一个匹配项的下标
29.LeetCode 第29题:两数相除30.LeetCode 第30题:串联所有单词的子串31.LeetCode 第31题:下一个排列32.LeetCode 第32题:最长有效括号33.LeetCode 第33题:搜索旋转排序数组34.LeetCode 第34题:在排序数组中查找元素的第一个和最后一个位置35.LeetCode 第35题:搜索插入位置36.LeetCode 第36题:有效的数独37.LeetCode 第37题:解数独38.LeetCode 第38题:外观数列39.LeetCode 第39题:组合总和40.LeetCode 第40题:组合总和 II41.LeetCode 第41题:缺失的第一个正数LeetCode 第28题:找出字符串中第一个匹配项的下标
题目描述
给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。
难度
简单
题目链接
示例
示例 1:
输入:haystack = "sadbutsad", needle = "sad"
输出:0
解释:"sad" 在下标 0 和 6 处匹配。
第一个匹配项的下标是 0 ,所以返回 0 。
示例 2:
输入:haystack = "leetcode", needle = "leeto"
输出:-1
解释:"leeto" 没有在 "leetcode" 中出现,所以返回 -1 。
提示
- 1 <= haystack.length, needle.length <= 104
- haystack 和 needle 仅由小写英文字符组成
解题思路
方法:KMP算法
KMP算法是一种高效的字符串匹配算法,它通过预处理模式串来避免不必要的比较。
关键点:
- 构建next数组(部分匹配表)
- 利用已匹配信息加速匹配过程
- 避免不必要的回溯
具体步骤:
- 构建模式串的next数组
- 利用next数组进行匹配:
- 主串指针i不回溯
- 模式串指针j根据next数组移动
- 找到匹配或遍历完主串
时间复杂度:O(m + n)
空间复杂度:O(n),用于存储next数组
图解思路
KMP匹配过程示意图
步骤 | 主串位置 | 模式串位置 | 是否匹配 | 下一步操作 |
---|---|---|---|---|
1 | s[0] | p[0] | 不匹配 | 移动模式串 |
2 | s[1] | p[0] | 匹配 | 同时右移 |
3 | s[2] | p[1] | 匹配 | 同时右移 |
4 | s[3] | p[2] | 不匹配 | 查next数组 |
Next数组构建过程
索引i | 前缀集合 | 后缀集合 | next[i] |
---|---|---|---|
0 | - | - | -1 |
1 | s[0] | s[0] | 0 |
2 | s[0:1] | s[1:2] | 0 |
3 | s[0:2] | s[1:3] | 1 |
代码实现
public class Solution {
public int StrStr(string haystack, string needle) {
if (string.IsNullOrEmpty(needle)) return 0;
if (haystack.Length < needle.Length) return -1;
// 构建next数组
int[] next = BuildNext(needle);
// KMP匹配过程
int i = 0, j = 0;
while (i < haystack.Length && j < needle.Length) {
if (j == -1 || haystack[i] == needle[j]) {
i++;
j++;
} else {
j = next[j];
}
}
return j == needle.Length ? i - j : -1;
}
private int[] BuildNext(string pattern) {
int[] next = new int[pattern.Length];
next[0] = -1;
int i = 0, j = -1;
while (i < pattern.Length - 1) {
if (j == -1 || pattern[i] == pattern[j]) {
i++;
j++;
next[i] = j;
} else {
j = next[j];
}
}
return next;
}
}
执行结果
- 执行用时:64 ms
- 内存消耗:36.9 MB
代码亮点
- 🎯 使用KMP算法实现高效字符串匹配
- 💡 巧妙构建next数组优化匹配过程
- 🔍 利用已匹配信息避免重复比较
- 🎨 代码结构清晰,变量命名规范
常见错误分析
- 🚫 没有正确处理边界情况
- 🚫 next数组构建错误
- 🚫 匹配过程中指针移动错误
- 🚫 返回值计算错误
解法对比
解法 | 时间复杂度 | 空间复杂度 | 优点 | 缺点 |
---|---|---|---|---|
暴力匹配 | O(m*n) | O(1) | 实现简单 | 效率低 |
KMP算法 | O(m+n) | O(n) | 效率高 | 实现复杂 |
Sunday算法 | O(m*n) | O(1) | 实现简单 | 最坏情况效率低 |
相关题目
- LeetCode 459. 重复的子字符串 - 简单
- LeetCode 214. 最短回文串 - 困难
- LeetCode 686. 重复叠加字符串匹配 - 中等
- LeetCode 796. 旋转字符串 - 简单
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了