-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement-strstr.cpp
More file actions
34 lines (31 loc) · 1.1 KB
/
implement-strstr.cpp
File metadata and controls
34 lines (31 loc) · 1.1 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
// Source : https://leetcode.com/problems/implement-strstr/
// Author : RQY
// Date : 2016-09-25
/**********************************************************************************
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
**********************************************************************************/
class Solution {
public:
int strStr(string haystack, string needle) {
if(haystack.length() == 0 && needle.length() == 0)
return 0;
for(int i = 0; i < haystack.length(); ++i){
if(haystack.length() - i < needle.length())
break;
if(strncmp(haystack.substr(i), needle, needle.length()))
return i;
}
return -1;
}
bool strncmp(string a, string b, size_t len){
size_t i;
for(i = 0; i < len && i < a.length() && i < b.length(); ++i){
if(a[i] != b[i])
return false;
}
if(i == len)
return true;
return false;
}
};