-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path044-WildcardMatching.cs
41 lines (39 loc) · 1.27 KB
/
044-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
//-----------------------------------------------------------------------------
// Runtime: 160ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _044_WildcardMatching
{
public bool IsMatch(string s, string p)
{
int sIndex = 0, pIndex = 0;
int lastSIndex = -1, lastPIndex = -1;
while (sIndex < s.Length)
{
if (pIndex < p.Length && (p[pIndex] == '?' || p[pIndex] == s[sIndex]))
{
sIndex++;
pIndex++;
}
else if (pIndex < p.Length && p[pIndex] == '*')
{
pIndex++;
if (pIndex == p.Length) { return true; }
lastSIndex = sIndex;
lastPIndex = pIndex;
}
else
{
if (lastSIndex == -1) { return false; }
sIndex = ++lastSIndex;
pIndex = lastPIndex;
}
}
while (pIndex < p.Length && p[pIndex] == '*') { pIndex++; }
return pIndex == p.Length && sIndex == s.Length;
}
}
}