-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirst_Occurrence_String.cpp
42 lines (40 loc) · 1.41 KB
/
First_Occurrence_String.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
using namespace std;
class Solution {
public:
int strStr(string haystack, string needle) {
if ((needle == " ") || (haystack == " ") || (needle.length() > haystack.length())){
return -1;
}
char first_letter = needle[0];
//first occurrance
int fo = 0;
int traverse = 0;
while ((abs(fo) < haystack.length()) && (traverse < haystack.length())) {
// cout << "1 " << fo << endl;
// cout << "traverse " << traverse << endl;
// cout << haystack[traverse] << " == " << first_letter << endl;
if (haystack[traverse] == first_letter){
int temp_traverse = traverse;
fo = traverse;
for (int i = 0; i < needle.length(); ++i){
if (haystack[temp_traverse] != needle[i]){
fo = -1;
break;
}
temp_traverse++;
}
if (fo != -1){
return fo;
} else {
traverse = traverse + 1;
// cout << "2 " << fo << endl;
}
} else {
traverse++;
}
}
// cout << (fo < haystack.length()) << endl;
// cout << haystack.length() << " fo " << fo << " traverse " << traverse << endl;
return -1;
}
};