akashtadwai

wildcard_matching

Jul 28th, 2021
739
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     bool isMatch(const string &s,const string &p, int i=0, int j=0) {
  4.         int n=s.size(),m=p.size();
  5.         string s_sub = (i<n)?s.substr(i):"",p_sub =(j<m)?p.substr(j):"";
  6.         if(p_sub.empty()) return s_sub.empty();
  7.         if(j<m and (p[j]=='?' or (i<n and p[j]==s[i] )))
  8.                 return isMatch(p,s,i+1,j+1);
  9.         if (j<m and p[j] == '*')
  10.                 return (isMatch(s,p,i+1,j) || isMatch(s,p,i,j+1));
  11.   return false;
  12.     }
  13. };
RAW Paste Data