using System;
using System.Collections.Generic;
class Gfg
{
class DP {
public bool value;
public char ch;
public DP(bool value, char ch)
{
this.value=value;
this.ch=ch;
}
}
static bool strmatch(string str, string pattern, int n, int m)
{
if (m == 0)
return (n == 0);
if (pattern[0] == '+')
return false;
DP[,] lookup=new DP[m+1, n+1];
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
lookup[i,j]=new DP(false, ' ');
}
}
lookup[0,0].value = true;
for (int j = 1; j <= n; j++)
if (pattern[j - 1] == '*')
lookup[j,0].value =
lookup[j - 1,0].value;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (pattern[i - 1] == '*') {
lookup[i,j].value =
lookup[i,j - 1].value ||
lookup[i - 1,j].value;
lookup[i,j].ch = str[j - 1];
}
else if (pattern[i - 1] == '?') {
lookup[i,j].value =
lookup[i - 1,j - 1].value;
lookup[i,j].ch = str[j - 1];
}
else if (str[j - 1] == pattern[i - 1])
lookup[i,j].value =
lookup[i - 1,j - 1].value;
else if (pattern[i - 1] == '+')
if (pattern[i - 2] != '+' ||
pattern[i - 2] != '*' ||
pattern[i - 2] != '?')
if (pattern[i - 2] == str[j - 1]) {
lookup[i,j].value =
lookup[i - 1,j - 1].value;
lookup[i,j].ch = str[j - 1];
}
else if (str[j-1] == lookup[i-1,j-1].ch) {
lookup[i,j].value =
lookup[i - 1,j - 1].value;
lookup[i,j].ch =
lookup[i - 1,j - 1].ch;
}
else
lookup[i,j].value = false;
}
}
return lookup[m,n].value;
}
public static void Main(string[] args)
{
string str = "baaabaaa";
string pattern = "*****+ba***+";
if (strmatch(str, pattern, str.Length, pattern.Length))
Console.Write("Yes");
else
Console.Write("No");
}
}