Pattern searching | Practice | GeeksforGeeks

Given a string str and a pattern pat. You need to check whether pattern is present or not in the given string. 
Input Format: The input line contains T, denoting the number of testcases. Each testcase contains two lines. First line contains string str. Second line contains patter text.
Output format: For each testcase in new line you have to print the "Present" if found otherwise "Not present".
User task: You need to complete the function searchPattern() containing string and text as an argument. You don't have to worry about input. 
Constraints:
1 <= T <= 100
1 <= |str| <= 30
1 <= |text| <= 30
Sample Input:
2
abcdefh
bcd
axzy
xy
Sample Output:
Present
Not present
 
0
hrithikraina20011 week ago
int a= str.find(pat);
    return a==-1?0:1;
0
luffysama11 month ago
bool searchPattern(string str, string pat){   // your code here   // your code here      int d = 256;   int q = 101;      int p = 0;   int t = 0 ;      int n = str.size();   int m = pat.size();      int i;   int j;      int h = 1;      for(i = 0 ; i< m-1 ; i++){       h=(h*d)%q;   }      for(i = 0 ; i < m ; i++){       p = (d*p + pat[i])%q;       t = (d*t + str[i])%q;   }      for(i = 0 ; i <= n-m ; i++){       if(p==t){           bool flag =true;           for(j = 0 ; j < m ; j++){               if(str[i+j]!=pat[j]){                   flag = false;                   break;               }           }                      if(j==m)               return true;       }                           if(i<n-m){           t = (d*(t-str[i]*h)+str[i+m])%q;                      if(t<0){               t+=q;           }       }   }      return false;} 
0
luffysama11 month ago
Can anyone show using Rabin Karp algorithm?
 
0
ram210320011 month ago
bool searchPattern(string str, string pat){   // your code here    int a=-1;  a=str.find(pat);  if(a!=-1)   return 1;   else  return 0;}
0
pappukumarmat212 months ago
bool searchPattern(string str, string pat)
{
    // your code here
    if(strstr(str.c_str(),pat.c_str())){
        return 1;
    }
    return false;
}
0
minturajmdb19993 months ago
bool searchPattern(string str, string pat){   int a=-1;   a=str.find(pat);   if(a!=-1)    return 1;    else   return 0;}
0
ayushnautiyal11104 months ago
C++ IMPLEMENTATION :)
TIME 0.0/1.1
IF/ELSE CODE :) 
😁😂😂😂😂😂😂😂😂😂😂😂😂😂😁
bool searchPattern(string str, string pat){   if(str.find(pat)==string::npos){       return false;   }   else{       return true;   }}
0
praveen marandi4 months ago
Why is Node driver program typecasting it to number? When the desired output is a string>
0
moninmodi4 months ago
int m=str.length();   int n=pat.length();   int t[m+1][n+1];   for(int i=0;i<m+1;i++){       for(int j=0;j<n+1;j++){           if(i==0||j==0)           t[i][j]=0;           else{               if(str[i-1]==pat[j-1]){                   t[i][j]=1+t[i-1][j-1];               }               else{                   t[i][j]=0;               }           }       }   }   int max=0;   for(int i=0;i<m+1;i++){       for(int j=0;j<n+1;j++){           if(t[i][j]>max){               max=t[i][j];           }       }   }   if(max==n)   return true;   else   return false;
0
kumarnitesh884414 months ago
bool searchPattern(string str, string pat){   // your code here   int n=str.length();   int m=pat.length();   for(int i=0;i<=n-m;i++){       int j;       for(j=0;j<m;j++){           if(str[i+j]!=pat[j]){               break;           }       }       if(m==j){           return true;              }   }   return false;} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.