-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28.cpp
More file actions
69 lines (58 loc) · 1.94 KB
/
28.cpp
File metadata and controls
69 lines (58 loc) · 1.94 KB
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
// Problem : 28. Implement strStr()
// Link : https://leetcode.com/problems/implement-strstr/
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <string>
using namespace std;
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle.empty())
return 0;
if (haystack.empty() && !needle.empty())
return -1;
if (haystack.size() < needle.size())
return -1;
if (haystack.size() == 1)
if (haystack[0] == needle[0])
return 0;
else
return -1;
for (int i = 0; i < haystack.size(); i++) {
if (haystack[i] == needle[0]) {
int left = i;
int right = left + needle.size() - 1;
int next = left;
if (right > haystack.size() - 1) { return -1; }
while (left >= 0 && right <= haystack.size() - 1 && left <= right) {
if (next == i && haystack[left] == needle[0])
next = left;
if (haystack[left] == needle[left - i] && haystack[right] == needle[right - i]) {
left++;
right--;
}
else
break;
}
/*
for odd length needles the left will be ahead and right will be behind the centre char
or even length needles the left will be 1 step ahead of the right
so check for that condition
*/
if (left - (needle.size() % 2 + 1) == right)
return i;
// search from the next ptr
if (next != i)
i = next - 1;
}
}
// if not found return -1
return -1;
}
};
int main() {
Solution ob;
cout << ob.strStr("aaaaa", "aab");
return 0;
}