LeetCode44.通配符匹配
题目
代码
public class Solution
{
public bool IsMatch(string s, string p)
{
if (string.IsNullOrEmpty(p))
return string.IsNullOrEmpty(s) ? true : false;
//简化了几种有空串可能的判断
int i = 0, j = 0;
int istart = -1, jstart = -1, len = p.Length;
while (i < s.Length) //匹配s串
{
//三种可能为true的匹配情况
if (j < len && (s[i] == p[j] || p[j] == '?'))
{
i++;
j++;
}
else if (j < len && p[j] == '*')
{
istart = i;
jstart = j;
j++;
}
else if (istart > -1)
{
i = istart + 1;
istart = i;
j = jstart + 1;
}
else
return false;
}
//当s遍历完后,判断p是否为空或者剩余的都为*
while (j < len && p[j] == '*')
j++;
return j == len;
}
}