Implement wildcard pattern matching with support for '?'
and '*'
.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
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
public boolean isMatch(String s, String p) {
if(s==null&&p==null)
return true;
if(s==null||p==null)
return false;
int i = 0,j=0;
int a = -1,b=0;
while(i<s.length()&&j<=p.length())
{
if(j<p.length()&&p.charAt(j)=='*')
{
if(j==p.length()-1)
return true;
a = i;
b = ++j;
}
else if(j<p.length()&&(p.charAt(j)=='?'||s.charAt(i)==p.charAt(j)))
{
i++;
j++;
}
else if(a>=0)
{
i = ++a;
j = b;
}
else
return false;
}
while(j<p.length()&&p.charAt(j)=='*')
j++;
if(i==s.length()&&j==p.length())
return true;
else
return false;
}
}