forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain6.cpp
54 lines (41 loc) · 1.25 KB
/
main6.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
/// Source : https://leetcode.com/problems/implement-strstr/
/// Author : liuyubobobo
/// Time : 2019-03-12
#include <iostream>
#include <vector>
using namespace std;
/// BM Algorithm
/// Only bad character heuristic is used
///
/// Time Complexity: O(m * all_characters + n)
/// Sapce Complexity: O(m * all_characters)
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle == "") return 0;
if(haystack == "") return -1;
int n = haystack.size(), m = needle.size();
vector<int> bad(256, -1); // last occurrence of every character
for(int i = 0; i < m; i ++)
bad[needle[i]] = i;
int i = 0;
while(i <= n - m){
int j = m - 1;
for(; j >= 0; j --)
if(needle[j] != haystack[i + j])
break;
if(j < 0) return i;
if(bad[haystack[i + j]] < 0)
i += j + 1;
else
i += max(1, j - bad[haystack[i + j]]);
}
return -1;
}
};
int main() {
cout << Solution().strStr("hello", "ll") << endl; // 2
cout << Solution().strStr("aaaaa", "bba") << endl; // -1
cout << Solution().strStr("mississippi", "issip") << endl; // 4
return 0;
}