-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathisMatch2.go
More file actions
46 lines (40 loc) · 1.04 KB
/
Copy pathisMatch2.go
File metadata and controls
46 lines (40 loc) · 1.04 KB
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
/* https://leetcode.com/problems/wildcard-matching/description/
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
*/
package ldp
func isMatch2(s string, p string) bool {
si, pi, match, stari := 0, 0, 0, -1
for si < len(s) {
if pi < len(p) && (p[pi] == '?' || s[si] == p[pi]) {
si++
pi++
} else if pi < len(p) && p[pi] == '*' {
stari = pi
match = si
pi++
} else if stari != -1 {
pi = stari + 1
match++
si = match
} else {
return false
}
}
for pi < len(p) && p[pi] == '*' {
pi++
}
return pi == len(p)
}