-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path62_haystackburn.cpp
More file actions
29 lines (28 loc) · 929 Bytes
/
62_haystackburn.cpp
File metadata and controls
29 lines (28 loc) · 929 Bytes
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
class Solution {
public:
int strStr(string haystack, string needle) {
int ans=-1;
int k=0;
int niddilesize=needle.size();
while(k<haystack.size()){
// Finding first character of needle in haystack .
auto itr=find(haystack.begin()+k,haystack.end(),needle[0]);
int n=itr-haystack.begin();
// If first character is found extract its index , compare it with substring of haystick .
string str=haystack.substr(n,niddilesize);
// If substring is equal to needle then this is our target so return its location.
if(str==needle){
ans=n;
break;
}
else
// If not then skip this and go ahead . now find needle[0] from one index greater than needle[0].
k=k+1;
continue;
}
if(ans==-1)
return -1;
else
return ans;
}
};