-
Notifications
You must be signed in to change notification settings - Fork 154
/
wildcard-matching.js
82 lines (72 loc) · 1.73 KB
/
wildcard-matching.js
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
/**
* Wildcard Matching
*
* 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
*/
/**
* Recursive Solution
*
* @param {string} s
* @param {string} p
* @return {boolean}
*/
const isMatch = (s, p) => {
if (p === '') {
return s === '';
}
if (s === '') {
return p === '*'.repeat(p.length);
}
if (p[0] === '*') {
return isMatch(s, p.substring(1)) || isMatch(s.substring(1), p);
} else {
return (s[0] === p[0] || p[0] === '?') && isMatch(s.substring(1), p.substring(1));
}
};
/**
* Dynamic Programming Solution
*
* @param {string} s
* @param {string} p
* @return {boolean}
*/
const isMatchDP = (s, p) => {
const sLen = s.length;
const pLen = p.length;
const dp = [];
for (let i = 0; i <= sLen; i++) {
dp[i] = Array(pLen + 1).fill(false);
}
dp[0][0] = true;
for (let j = 1; j <= pLen; j++) {
dp[0][j] = p[j - 1] === '*' && dp[0][j - 1];
}
for (let i = 1; i <= sLen; i++) {
for (let j = 1; j <= pLen; j++) {
if (p[j - 1] === '*') {
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
} else {
dp[i][j] = (s[i - 1] === p[j - 1] || p[j - 1] === '?') && dp[i - 1][j - 1];
}
}
}
return dp[sLen][pLen];
};
export { isMatch, isMatchDP };