-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathWildcardMatching.cs
executable file
·69 lines (65 loc) · 3.79 KB
/
WildcardMatching.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
// Source : https://leetcode.com/problems/wildcard-matching/
// Author : codeyu
// Date : Monday, October 31, 2016 12:18:24 AM
/**********************************************************************************
*
* Implement wildcard pattern matching with support for '?' and '*'.
*
*
* '?' Matches any single character.
* '*' Matches any sequence of characters (including the empty sequence).
*
* The matching should cover the entire input string (not partial).
*
* The function prototype should be:
* bool isMatch(const char *s, const char *p)
*
* Some examples:
* isMatch("aa","a") ? false
* isMatch("aa","aa") ? true
* isMatch("aaa","aa") ? false
* isMatch("aa", "*") ? true
* isMatch("aa", "a*") ? true
* isMatch("ab", "?*") ? true
* isMatch("aab", "c*a*b") ? false
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution044
{
public static bool IsMatch(string s, string p)
{
if (p.Length == 0)
return s.Length == 0;
bool[] res = new bool[s.Length + 1];
res[0] = true;
for (int j = 0; j < p.Length; j++)
{
if (p[j] != '*')
{
for (int i = s.Length - 1; i >= 0; i--)
{
res[i + 1] = res[i] && (p[j] == '?' || s[i] == p[j]);
}
}
else
{
int i = 0;
while (i <= s.Length && !res[i])
i++;
for (; i <= s.Length; i++)
{
res[i] = true;
}
}
res[0] = res[0] && p[j] == '*';
}
return res[s.Length];
}
}
}