Leetcode wildcard matching c++

Leetcode wildcard matching c++ (Link: wildcard matching c++ problem): Implement wildcard pattern matching with support for ‘?’ and ‘*’.

‘?’ Matches any single character.
‘*’ Matches any sequence of characters (including the emptysequence).

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch(“aa”,”a”) → false
isMatch(“aa”,”aa”) → true
isMatch(“aaa”,”aa”) → false
isMatch(“aa”, “*”) → true
isMatch(“aa”, “a*”) → true
isMatch(“ab”, “?*”) → true
isMatch(“aab”, “c*a*b”) → false

string_matching
C++ code is as follows:
bool isMatch(const char *s, const char *p) {
const char* star=NULL;
const char* ss=s;
while (*s){
if ((*p==’?’)||(*p==*s)){s++;p++;continue;} //If *s==*p or *p == ?
which means this is a match, then goes to next element s++ p++.

if (*p==’*’){star=p++; ss=s;continue;}//save this *’s position and the matched s position
if (star){ p = star+1; s=++ss;continue;}//if there is an *, set current p to the next element of *,
and set current s to the next saved s position until there is a match

return false;//if there is no *, return false;
}
while (*p==’*’){p++;}
return !*p;
}

 Median of two sorted arrays

One thought on “Leetcode wildcard matching c++”

Leave a Reply

Your email address will not be published. Required fields are marked *