using System;
using System.Collections.Generic;
using System.Text;
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串
* @param pattern string字符串
* @return bool布尔型
*/
public bool match (string str, string pattern) {
pattern = preProcess(pattern);
Console.WriteLine(pattern);
bool[,] dp = new bool[str.Length + 1, pattern.Length + 1];
dp[0,0] = true;
for(int i = 0; i <= str.Length; i++){
Console.WriteLine();
Console.WriteLine("i:" + i + " ");
for(int j = 1; j <= pattern.Length; j++){
if(pattern[j - 1] == '*'){
j++;
dp[i, j] = (j >= 2 && dp[i, j - 2]) || (i > 0 && dp[i - 1, j] && (str[i - 1] == pattern[j - 1] || (pattern[j - 1] == '.')));
}
else{
if(pattern[j - 1] == '.' && i > 0) dp[i, j] = dp[i - 1, j - 1];
else if(i > 0 && str[i - 1] == pattern[j - 1]) dp[i, j] = dp[i - 1, j - 1];
}
Console.Write("j:"+ j + " " + "bool:" + dp[i,j]);
}
}
return dp[str.Length, pattern.Length];
}
public string preProcess(string pattern){
StringBuilder sb = new StringBuilder(pattern);
int i = 0;
while(i < sb.Length){
if(sb[i] == '*'){
char a = sb[i - 1];
sb[i] = sb[i - 1];
sb[i - 1] = '*';
if(a == '.'){
i++;
continue;
}
//while(i + 1 < sb.Length && (sb[i + 1] == a || sb[i + 1] == '*')) sb.Remove(i + 1, 1);
}
i++;
}
return sb.ToString();
}
}