Skip to content

Commit

Permalink
Update 2301.Match-Substring-After-Replacement_KMP.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Jun 12, 2022
1 parent 96b1600 commit 8001bc6
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Solution {
bool table[128][128];

public:
bool match(char x, char y)
bool equal(char x, char y)
{
return (x==y || table[y][x]);
}
Expand All @@ -26,16 +26,16 @@ class Solution {
vector<int> suf = preprocess(needle);

vector<int>dp(n,0);
dp[0] = match(haystack[0], needle[0]);
dp[0] = equal(haystack[0], needle[0]);
if (m==1 && dp[0]==1)
return 0;

for (int i=1; i<n; i++)
{
int j = dp[i-1];
while (j>0 && !match(haystack[i], needle[j]))
while (j>0 && !equal(haystack[i], needle[j]))
j = suf[j-1];
dp[i] = j + match(haystack[i], needle[j]);
dp[i] = j + equal(haystack[i], needle[j]);
if (dp[i]==needle.size())
return i-needle.size()+1;
}
Expand Down

0 comments on commit 8001bc6

Please sign in to comment.