Implement strstr | Practice | GeeksforGeeks

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 in s (0 based indexing).
Note: You are not allowed to use inbuilt function.
 
Example 1:
Input:
s = GeeksForGeeks, x = Fr
Output: -1
Explanation: Fr is not present in the
string GeeksForGeeks as substring.

 
Example 2:
Input:
s = GeeksForGeeks, x = For
Output: 5
Explanation: For is present as substring
in GeeksForGeeks from index 5 (0 based
indexing).

 
Your Task:
You don't have to take any input. Just complete the strstr() function which takes two strings str, target as an input parameter. The function returns -1 if no match if found else it returns an integer denoting the first occurrence of the x in the string s.
 
Expected Time Complexity: O(|s|*|x|)
Expected Auxiliary Space: O(1)
Note : Try to solve the question in constant space complexity.
 
Constraints:
1 <= |s|,|x| <= 100
0
harshilrpanchal199814 hours ago
java solution
 
 int xlen = x.length();     for(int i = 0; i < s.length(); ++i) {         if(s.charAt(i) == x.charAt(0)) {             int j;             for(j = 0; j < x.length(); j++) {                 if(i+j < s.length()) {                      if(s.charAt(i+j) != x.charAt(j)) {                          break;                      }                 } else {                     break;                 }             }             if(j == x.length()) {                 return i;             }         }     }     return -1;
-1
abhikulkarni14151 day ago
class GfG{    int strstr(String s, String x)   {      return s.indexOf(x);   }}
0
atharvaminajgi6 days ago
int strstr(string s, string x){    //Your code here    int res = s.find(x);    if(res != string::npos){        return res;    }    return -1;}
0
gfgraj1 week ago
int strstr(string s, string x){    int n=s.length();    int m=x.length();    int j,i,flag=-1;    for(i=0;i<n;i++){        if(s[i]==x[0]){            for(j=0;j<m;j++)            {                if(s[i+j]!=x[j])                   break;            }            if(j==m){                flag=i;                break;            }        }    }    return flag;}
0
visionsameer391 week ago
IN PYTHON
    
def strstr(s,x):   return s.find(x)
+1
nitishmishra9372 weeks ago
    int strstr(String s, String x)   {      // return s.indexOf(x);      int xlen = x.length();      for(int i = 0; i < s.length(); ++i) {          if(s.charAt(i) == x.charAt(0)) {              int j;              for(j = 1; j < x.length(); ++j) {                  if(i+j < s.length()) {                       if(s.charAt(i+j) != x.charAt(j)) {                           break;                       }                  } else {                      break;                  }              }              if(j == x.length()) {                  return i;              }          }      }      return -1;   }
+1
muneeralam3202 weeks ago
PYTHON SOLUTION
def strstr(s,x):
    #code here
    return s.find(x)
0
deeplakhotia8762 weeks ago
1 LINE SOLUTION
 
return s.find(x);
 
Upvote if you find it useful.
-1
nischithtn2 weeks ago
int strstr(String s, String x)   {       int p=-1;      if(s.contains(x)){          p=s.indexOf(x);      }      return p;   }
0
kavajay98
This comment was deleted.
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.