Implement strstr
PROBLEM :
Your task is to implement the function strstr. The function takes two strings as arguments(s,x) and locates the occurrence of the string x in the string s. The function returns and integer denoting the first occurrence of the string x .
Input:
The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. The first line of each test case contains two strings str and target.
Output:
For each test case in a new line output will be an integer denoting the first occurrence of the target string in the string s. Return -1 if no match found.
Constraints:
1<=T<=100
1<=length of (s,x)<=1000
Example:
Input
2
GeeksForGeeks Fr
GeeksForGeeks For
Output
-1
5
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( using KMP algo )
--------------------------------------------------------------------------------
/* The function should return position where the target string
matches the string str
Your are required to complete this method */
void prefix_table(string ,int [],int ) ;
int strstr(string s, string x)
{
int l1,l2 ;
l1=s.length() ;
l2=x.length() ;
int arr[l2] ;
prefix_table(x,arr,l2) ;
int i,j ;
i=0;
j=0 ;
while(i<l1)
{
if(s[i]==x[j])
{
if(j==l2-1)
return i-j ;
else
{
i++ ;
j++ ;
}
}
else if(j>0)
j=arr[j-1] ;
else
i++ ;
}
return -1 ;
}
void prefix_table(string str,int arr[],int len)
{
int i,j ;
i=1 ;
j=0 ;
arr[j]=0 ;
while(i<len)
{
if(str[i]==str[j])
{
arr[i]=j+1 ;
j++ ;
i++ ;
}
else if(j==0)
{
arr[i]=0 ;
i++ ;
}
else
{
j=arr[j-1] ;
}
}
}
---------------------------------------------------------------------------------
Your task is to implement the function strstr. The function takes two strings as arguments(s,x) and locates the occurrence of the string x in the string s. The function returns and integer denoting the first occurrence of the string x .
Input:
The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. The first line of each test case contains two strings str and target.
Output:
For each test case in a new line output will be an integer denoting the first occurrence of the target string in the string s. Return -1 if no match found.
Constraints:
1<=T<=100
1<=length of (s,x)<=1000
Example:
Input
2
GeeksForGeeks Fr
GeeksForGeeks For
Output
-1
5
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( using KMP algo )
--------------------------------------------------------------------------------
/* The function should return position where the target string
matches the string str
Your are required to complete this method */
void prefix_table(string ,int [],int ) ;
int strstr(string s, string x)
{
int l1,l2 ;
l1=s.length() ;
l2=x.length() ;
int arr[l2] ;
prefix_table(x,arr,l2) ;
int i,j ;
i=0;
j=0 ;
while(i<l1)
{
if(s[i]==x[j])
{
if(j==l2-1)
return i-j ;
else
{
i++ ;
j++ ;
}
}
else if(j>0)
j=arr[j-1] ;
else
i++ ;
}
return -1 ;
}
void prefix_table(string str,int arr[],int len)
{
int i,j ;
i=1 ;
j=0 ;
arr[j]=0 ;
while(i<len)
{
if(str[i]==str[j])
{
arr[i]=j+1 ;
j++ ;
i++ ;
}
else if(j==0)
{
arr[i]=0 ;
i++ ;
}
else
{
j=arr[j-1] ;
}
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment