LeetCode10.正则表达式匹配
题目
代码
public class Solution
{
public bool IsMatch(string s, string p)
{
bool[,] sp = new bool[s.Length + 1, p.Length + 1];
sp[s.Length, p.Length] = true;
for(int i=s.Length;i>=0;i--)
{
for(int j=p.Length-1;j>=0;j--)
{
bool firstmatch = i < s.Length && (p[j] == s[i] || p[j] == '.');
if(j+1<p.Length&&p[j+1]=='*')
sp[i, j] = sp[i, j + 2] || firstmatch && sp[i + 1,j];
else
sp[i, j] = firstmatch && sp[i + 1, j + 1];
}
}
return sp[0, 0];
}
}