-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ_10.cs
123 lines (96 loc) · 3.12 KB
/
Q_10.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
public class Solution
{
// public static void Main(string[] args)
// {
// IsMatch("aab", "c*a*b");
// }
public bool IsMatch(string s, string p)
{
if (s == null || p == null)
{
return false;
}
bool [,] dp = new bool[s.Length + 1, p.Length + 1];
dp[0, 0] = true;
for(int x = 1; x <= s.Length; x++)
{
dp[x, 0] = false;
}
Print(dp, s, p);
for(int y = 0; y < p.Length; y++)
{
if (p[y] == '*' && dp[0, y-1]) {
dp[0, y+1] = true;
}
}
Print(dp, s, p);
// for (int i = 0 ; i < s.Length; i++)
// {
// for (int j = 0; j < p.Length; j++)
// {
// // Console.WriteLine("Matching {0} with {1}", s[i], p[j]);
// dp[i+1, j+1] = CanMatch(s[i], p[j]) && dp[i,j];
// if (p[j] == '*')
// {
// //if (p[j-1] != s[i] && p[j-1] != '.')
// //{
// dp[i+1, j+1] = dp[i+1, j-1];
// //
// if(CanMatch(s[i], p[j-1]))
// {
// dp[i+1, j+1] = dp[i+1, j] || dp[i, j+1] || dp[i+1, j-1];
// }
// }
// }
// }
for(int x = 0; x < s.Length; x++)
{
for(int y = 0; y < p.Length; y++)
{
Console.WriteLine("Matching {0} with {1}", s[x], p[y]);
dp[x+1, y+1] = CanMatch(s[x], p[y]) && dp[x, y];
//Console.WriteLine("here1");
if(p[y] == '*')
{
dp[x+1, y+1] = dp[x+1, y-1]; // if we ignore the element before *
if((CanMatch(s[x], p[y-1])))
{
dp[x+1, y+1] |= dp[x+1, y] // one occurence of element before *one occurence of element before *
|| dp[x, y+1] // multiple occurences of element before *
;//|| dp[x+1, y-1]; // not needed
}
//Console.WriteLine("here2");
}
}
//Print(dp, s, p);
}
return dp[s.Length, p.Length];
}
private bool CanMatch(char x, char y)
{
return x == y || y == '.';
}
private void Print(bool[,] dp, string s, string p)
{
Console.Write(" \" ");
for(int x = 0; x < s.Length; x++)
{
Console.Write(s[x] + " ");
}
Console.WriteLine();
for(int y = 0 ; y <= p.Length; y++)
{
for(int x = 0; x <= s.Length; x++)
{
if(x == 0)
{
if(y == 0) Console.Write("\" ");
else if(y <= p.Length) Console.Write(p[y-1] + " ");
}
Console.Write((dp[x, y] == true ? "T" : "F") + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
}