-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_occurence_of_s2_in_s1.cpp
47 lines (39 loc) · 1.01 KB
/
find_occurence_of_s2_in_s1.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
/*
link:- https://practice.geeksforgeeks.org/problems/implement-strstr/1
https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/
Input:
s = GeeksForGeeks, x = Fr
Output: -1
Explanation: Fr is not present in the
string GeeksForGeeks as substring.
*/
//NAIVE
int strStr(string a, string b) {
if(b.length()>a.length()) return -1;
for(int i=0;i<=a.length()-b.length();i++)
{
for(int j=0;j<b.length();j++)
{
if(a[i+j]!=b[j])
{
break;
}
if(j==b.length()-1) return i;
}
}
return -1;
}
//Function to locate the occurrence of the string x in the string s.
int strstr(string s, string x)
{
//Your code here
// char p=strstr(s,x);
if(s.find(x)!=string::npos)
{
return s.find(x); //returns the index of the first occurence of x in s
}
else
{
return -1;
}
}