Pattern Searching | Practice | GeeksforGeeks

Given a text and a pattern, the task is to check if the pattern exists in the text or not.
Example 1:
Input: text = "geeksforgeeks"
       pat = "geek"
Output: 1
Explanation: "geek" exits in
"geeksforgeeks"
â€‹Example 2:
Input: text = "geeksforgeeks"
pat = "gfg" 
Output: 0 
Explanation: "gfg" does not exits in 
"geeksforgeeks"

Your Task:  
You don't need to read input or print anything. Your task is to complete the function search() which takes the string two strings as inputs and returns 1 if the pattern is found, otherwise 0.

Expected Time Complexity: O(|text| + |pat|)
Expected Auxiliary Space: O(|text| + |pat|)

Constraints:
1 ≤ |text|, |pat| ≤ 105
Both the string contains lower case English alphabets
0
singhvipnesh63 days ago
class Solution {   int search(String text, String pat) {       HashMap<Character,Integer>map=new HashMap<>();            for(int i=0;i<text.length();i++)       {           char ch=text.charAt(i);           if(!map.containsKey(ch))           map.put(ch,i);           else            map.put(ch,map.get(ch)+1);       }       if(text.contains(pat))       {           for(int i=0;i<pat.length();i++)           {               char ch=pat.charAt(i);               if(map.containsKey(ch)){               return 1;               }           }       }        return 0;   }};
+1
rajatgupta96962 weeks ago
 // C++ Naive Algorithm solution
   if(text.length()<pat.length())
	        return 0; 
	        //   if text length is smaller
	        
	    for(int i=0;i<text.length()-pat.length()+1;i++)
	    {				  
	          int count=0;
	        for(int j=0;j<pat.length();j++)
	        {
	            if(pat[j]==text[i+j])
	                count++;
	             else{
	                 break;
	                  // if mismatch  break inner loop
	             }
	        }
	        if(count==pat.length())
	            return 1;  // for matching
	    }
	    return 0;
0
aakasshuit1 month ago
//Java Solution

        if(pat.length()>text.length()){
            return 0;
        }
        if(text.contains(pat)){
            return 1;
        }
        return 0;
+1
kronizerdeltac3 months ago
JAVA : Using Substring Method
 
class Solution {   int search(String text, String pat) {       if(pat.length() > text.length())           return 0;       return text.contains(pat) ? 1 : 0;   }};
0
raghavendra7774 months ago
 k=text.count(pat) if k!=0:     return 1 return 0  
0
prashspp4 months ago
python solution:
  
import reclass Solution:def search(self, text, pat): if re.search(pat,text):     return 1 else:     return 0
0
imranwahid4 months ago
C++ solution using KMP algorithm
 
First build the lps array.
First build the lps array.
 
2. Then apply KMP algorithm
+1
pketul22125 months ago
class Solution {
    int search(String text, String pat) {
        
        if(text.indexOf(pat) == -1)
            return 0;
        
        return 1;
    }
}
0
swaritkhare6 months ago
class Solution {   int search(String text, String pat) {       // code here       if(text.toLowerCase().contains(pat.toLowerCase()) == true){           return 1;       } else{           return 0;       }   }};
0
piyushyadav56 months ago
int search(string text, string pat){    // Your code goes here    if(text.find(pat) !=string::npos)    {        return 1;    }    return 0;}
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.