RegEx  matching | Practice | GeeksforGeeks

Given a pattern string and a test string, If the pattern is preceded by a ^, the pattern(excluding the ^) will be matched with the starting position of the text string. Similarly, if it is preceded by a $, the pattern(excluding the ^) will be matched with the ending position of the text string. If no such markers are present, it will be checked whether pattern is a substring of test.
 
Example 1:
Input:
P = "^coal"
S = "coaltar"
Output:
1
Explanation:
The pattern "coal" is present at the
beginning of the string, so Output is 1.

Example 2:
Input:
P = "tar$"
S = "coaltar"
Output:
1
Explanation: 
The pattern "tar" is present at the
end of the string, so Output is 1.
Example 3:
Input:
P = "rat"
S = "algorate"
Output:
1
Explanation:
The pattern "rat" is present as a
substring of the string S,
so Output is 1.
Example 4:
Input:
P = "abcd"
S = "efgh"
Output:
0
Explanation:
There is no substring which is equal
to P in string S, so output is 0.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isPatternPresent() which takes Strings P and S as input and returns 0 or 1.
 
Expected Time Complexity: O(|P|*|S|)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= |S| <= 105
1 <= |P| <= 10
String S may contain both Uppercase and Lowercase letters.
+1
aloksinghbais022 weeks ago
C++ solution having time complexity as O(|S|*|P|) and space complexity as O(1) is as follows :- 
 
Execution Time :- 0.02 / 1.04 sec
 
int isPatternPresent(string S, string P) {       if(P.length() > S.length()+1) return (0);       if(P.find("^") != -1){                      for(int i = 0,j = 1; i < S.length() && j < P.length(); i++,j++){               if(S[i] != P[j]) return (false);           }                      return (true);       }       else if(P.find("$") != -1){                      for(int i = S.length()-1, j = P.length()-2; i >= 0 && j >= 0; i--,j--){               if(S[i] != P[j]) return (false);           }                      return (true);       }              return S.find(P) != -1 ? 1 : 0;    }
 
0
neeleshkumar12 months ago
 int isPatternPresent(string S, string P) {              int m=S.size(),n=P.size();       if(n>m)return 0;              if(P[0]=='^'){                      P=P.substr(1);                      S=S.substr(0,n-1);                  }       else if(P[n-1]=='$'){           P=P.substr(0,n-1);           S=S.substr(m-n+1,n);                  }              if(S.find(P)!=string::npos)           return 1;           else return 0;                 }
0
akkeshri140420012 months ago
int isPatternPresent(string S, string P) {       // code here       int n=S.length();       int m=P.length();       if(m>n)       return 0;       if(P[0]=='^')       {           string s=P.substr(1);           string c=S.substr(0,m-1);                     return s==c? 1:0;           }       else if(P[m-1]=='$')       {           string s=P.substr(0,m-1);           string c=S.substr(n-m+1);                     return s==c? 1:0;       }       else{           for(int i=0;i<n;i++){               if(S.substr(i,m)==P){                   return 1;               }           }           return 0;       }   }
0
vishalchaudhary19753 months ago
Simple java Solution using REGEX
  static int isPatternPresent(String s, String p) {       // code here              int n1=s.length();       int n2=p.length();       char c=p.charAt(0);       char v=p.charAt(n2-1);              if(c=='^'){                      String temp1=p.substring(1,n2);           String regex=temp1+"[a-zA-Z]+";           if(s.matches(regex)){               return 1;           }           return 0;       }       else if(v=='$'){           String temp2=p.substring(0,n2-1);           String regex="[a-zA-Z]+"+temp2;           if(s.matches(regex)){               return 1;           }           return 0;       }       else{           if(s.contains(p)){               return 1;           }           return 0;       }
+2
harmlesspotato4 months ago

class Solution {
  public:
    int isPatternPresent(string S, string P) {
        string match="";
        int i=0;
        int n=S.length();
        int m=P.length();
        if(m>n)
        return 0;
        if(P[0]=='^')
        {
            string s=P.substr(1,m);
            string c=S.substr(0,m-1);
           
            return s==c? 1:0;
            }
        else if(P[m-1]=='$')
        {
            string s=P.substr(0,m-1);
            string c=S.substr(n-m+1,n);
           
            return s==c? 1:0;
        }
        else{
            return S.find(P)!=-1;
        }
    }
    
};


0
shisoni4559997 months ago
in a given input “G” is present ..
0
ANAS MALVAT9 months ago
ANAS MALVAT
        if(p[0] == '^'){            p = p.substr(1,p.length());            if(s.substr(0,p.length()) == p)                {                    return 1;                }                return 0;        }        else if(p[p.length() - 1] == '$')        {            p = p.substr(0,p.length() - 1);            if(s.substr(s.length() - p.length(),s.length()) == p)            {                return 1;            }            return 0;        }        else{            return s.find(p) != -1;        }
0
Baskaran S2 years ago
Baskaran S
Simple Java Solution :Execution Time:0.16Link
0
sahil Malik2 years ago
sahil Malik
c++ with 3 separate functionshttps://ide.geeksforgeeks.o...
0
BHASKAR JHA2 years ago
BHASKAR JHA
java solun
https://ide.geeksforgeeks.o...
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.