题目描述
给定字符串haystack
和needle
,判定needle
是否是haystack
的子串。以下给出的两种算法复杂度均为O(m)
。
传送门:Leetcode 28
KMP算法
首先计算needle
的next数组
,随后执行KMP算法。
class Solution
{
public:
int strStr(string haystack, string needle)
{
if (needle.empty())
return 0;
int m = haystack.size();
int n = needle.size();
// 'next' array of needle
vector<int> next(n);
next[0] = -1;
int j = -1;
for (int i = 1; i < n; ++i)
{
while (j != -1 && needle[i] != needle[j + 1])
j = next[j];
if (needle[i] == needle[j + 1])
++j;
next[i] = j;
}
// kmp algorithm
j = -1;
for (int i = 0; i < m; ++i)
{
while (j != -1 && haystack[i] != needle[j + 1])
j = next[j];
if (haystack[i] == needle[j + 1])
++j;
if (j == n - 1)
return i - n + 1;
}
return -1;
}
};
字符串哈希算法
哈希值 = 26进制字符串
class Solution
{
public:
int strStr(string haystack, string needle)
{
const long long mod = 10e9 + 7;
int m = haystack.length();
int n = needle.length();
if (m < n) return -1;
long long h_hash = 0;
long long n_hash = 0;
long long pow = 1; // 计算26^n
// 计算原始窗口的哈希值
for (int i = 0; i < n; ++i)
{
pow = (pow * 26) % mod;
h_hash = (h_hash * 26 + haystack[i] - 'a') % mod;
n_hash = (n_hash * 26 + needle[i] - 'a') % mod;
}
if(h_hash == n_hash) return 0;
// 移动窗口,动态计算哈希值
for (int l = 0, r = n; r < m; ++l, ++r)
{
h_hash = (h_hash * 26 - pow * (haystack[l] - 'a') + haystack[r] - 'a') % mod;
if(h_hash == n_hash) return l + 1;
}
return -1;
}
};