-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28.implement-str-str.cpp
119 lines (118 loc) · 2.92 KB
/
28.implement-str-str.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* @lc app=leetcode id=28 lang=cpp
*
* [28] Implement strStr()
*
* https://leetcode.com/problems/implement-strstr/description/
*
* algorithms
* Easy (31.36%)
* Total Accepted: 390.7K
* Total Submissions: 1.2M
* Testcase Example: '"hello"\n"ll"'
*
* Implement strStr().
*
* Return the index of the first occurrence of needle in haystack, or -1 if
* needle is not part of haystack.
*
* Example 1:
*
*
* Input: haystack = "hello", needle = "ll"
* Output: 2
*
*
* Example 2:
*
*
* Input: haystack = "aaaaa", needle = "bba"
* Output: -1
*
*
* Clarification:
*
* What should we return when needle is an empty string? This is a great
* question to ask during an interview.
*
* For the purpose of this problem, we will return 0 when needle is an empty
* string. This is consistent to C's strstr() and Java's indexOf().
*
*/
#if 1
// https://www.geeksforgeeks.org/kmp-algorithm-for-pattern-searching/
class Solution {
private:
void KMTPreProcess(vector<int> & outLps, string const & needle){
int longestPrefixSurfix = 0;
outLps.resize(needle.size(), 0);
for(int i = 1; i < needle.size();){
if(needle[i] == needle[longestPrefixSurfix]){
++longestPrefixSurfix;
outLps[i] = longestPrefixSurfix;
++i;
}
else if(longestPrefixSurfix){
longestPrefixSurfix = outLps[longestPrefixSurfix - 1];
}
else{
outLps[i] = 0;
++i;
}
}
}
public:
int strStr(string haystack, string needle) {
if(needle.empty())
return 0;
if(haystack.empty())
return -1;
if(haystack.size() < needle.size())
return -1;
int i,j;
bool match;
vector<int> lps;
KMTPreProcess(lps, needle);
for(i = 0, j = 0; i < haystack.size();){
if(haystack[i] == needle[j]){
++j;
++i;
}
if(j == needle.size()){
return i - j;
}
if(i < haystack.size() && haystack[i] != needle[j]){
j ? j = lps[j-1] : ++i;
}
}
return -1;
}
};
#endif
#if 0 // brute force time limit excceeded
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle.empty())
return 0;
if(haystack.empty())
return -1;
if(haystack.size() < needle.size())
return -1;
int i,j;
bool match;
for(i = 0; i < haystack.size() - needle.size() + 1; ++i){
match = true;
for(j = 0; j < needle.size(); ++j){
if(haystack[i + j] != needle[j]){
match = false;
break;
}
}
if(match)
return i;
}
return -1;
}
};
#endif