题目描述:实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
示例 1:
输入: haystack = “hello”, needle = “ll”
输出: 2
示例 2:
输入: haystack = “aaaaa”, needle = “bba”
输出: -1
说明:
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
解题思路
思路 1:调用Java库函数
class Solution {
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
}
思路 2:暴力法,时间复杂度:O((m-n)n),m 是主字符串,n 是模式字符串。
public int strStr(String haystack, String needle) {
if (needle.equals(""))
return 0;
int j = 0;
int length = needle.length();
for (int i = 0; i < haystack.length() - length + 1; i++) {
j = 0;
while (haystack.charAt(i + j) == needle.charAt(j)) {
j++;
if (j == length) {
return i;
}
}
}
return -1;
}
直接用substring()快了很多
class Solution {
public int strStr(String haystack, String needle) {
int len1 = haystack.length();
int len2 = needle.length();
if(len1 < len2) {
return -1;
} else if(0 == len2) {
return 0;
}
//len1 > len2
for(int i = 0; i < len1-len2+1; i++) {
if(haystack.substring(i,i+len2).equals(needle)) {
return i;
}
}
return -1;
}
}
思路 3: KMP 算法实现字符串匹配
kmp算法,让前面匹配的结果指导后面开始的匹配
参考答案
public int strStr(String haystack, String needle) {
int strLen = haystack.length(), subLen = needle.length();
if (subLen == 0) return 0;
if (strLen == 0) return -1;
// 构建状态机
int[][] FSM = new int[subLen][256];
int X = 0, match = 0;
for (int i = 0; i < subLen; i++) {
match = (int) needle.charAt(i);
for (int j = 0; j < 256; j++) {
// 当前状态 + 匹配失败字符 = 孪生词缀状态 + 匹配字符
FSM[i][j] = FSM[X][j];
}
FSM[i][match] = i + 1;
if (i > 0) {
// 下一孪生前缀状态 = X + match
X = FSM[X][match];
}
}
// 匹配子串
int state = 0;
for (int i = 0; i < strLen; i++) {
state = FSM[state][haystack.charAt(i)];
if (state == subLen) {
return i - subLen + 1;
}
}
return -1;
}