101 Logo
onenoughtone

Code Implementation

Wildcard Pattern Matcher Implementation

Below is the implementation of the wildcard pattern matcher:

solution.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Determine if a text string matches a given pattern with support for '?' and '*'.
* Dynamic Programming approach.
*
* @param {string} text - The input text string
* @param {string} pattern - The pattern to match against
* @return {boolean} - True if the text matches the pattern, false otherwise
*/
function isMatchDP(text, pattern) {
const m = text.length;
const n = pattern.length;
// Create a DP table
const dp = Array(m + 1).fill().map(() => Array(n + 1).fill(false));
// Empty pattern matches empty text
dp[0][0] = true;
// Handle patterns starting with '*'
for (let j = 1; j <= n; j++) {
if (pattern[j - 1] === '*') {
dp[0][j] = dp[0][j - 1];
}
}
// Fill the DP table
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (pattern[j - 1] === '?') {
// '?' matches any single character
dp[i][j] = dp[i - 1][j - 1];
} else if (pattern[j - 1] === '*') {
// '*' can match zero or more characters
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
} else {
// Regular character must match
dp[i][j] = (text[i - 1] === pattern[j - 1]) && dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
/**
* Determine if a text string matches a given pattern with support for '?' and '*'.
* Greedy approach with backtracking.
*
* @param {string} text - The input text string
* @param {string} pattern - The pattern to match against
* @return {boolean} - True if the text matches the pattern, false otherwise
*/
function isMatchGreedy(text, pattern) {
const m = text.length;
const n = pattern.length;
let i = 0; // pointer for text
let j = 0; // pointer for pattern
let starIdx = -1; // position of last '*' in pattern
let match = 0; // position in text corresponding to last '*'
while (i < m) {
// If current characters match or pattern has '?'
if (j < n && (pattern[j] === '?' || pattern[j] === text[i])) {
i++;
j++;
}
// If pattern has '*'
else if (j < n && pattern[j] === '*') {
starIdx = j;
match = i;
j++;
}
// If we've seen a '*' before, backtrack
else if (starIdx !== -1) {
j = starIdx + 1;
match++;
i = match;
}
// If none of the above conditions are met, no match
else {
return false;
}
}
// Skip any remaining '*' characters in pattern
while (j < n && pattern[j] === '*') {
j++;
}
// Return true if we've reached the end of the pattern
return j === n;
}
/**
* Determine if a text string matches a given pattern with support for '?' and '*'.
* Recursive approach with memoization.
*
* @param {string} text - The input text string
* @param {string} pattern - The pattern to match against
* @return {boolean} - True if the text matches the pattern, false otherwise
*/
function isMatchRecursive(text, pattern) {
// Create a memoization map
const memo = new Map();
function match(i, j) {
// Create a unique key for the current state
const key = i + ',' + j;
// Check if we've already computed this result
if (memo.has(key)) {
return memo.get(key);
}
// Base case: if we've reached the end of the pattern
if (j === pattern.length) {
return i === text.length;
}
// Base case: if we've reached the end of the text
if (i === text.length) {
// Remaining pattern must be all '*'
for (let k = j; k < pattern.length; k++) {
if (pattern[k] !== '*') {
return false;
}
}
return true;
}
let result;
// Current character in pattern
if (pattern[j] === '?') {
// '?' matches any single character
result = match(i + 1, j + 1);
} else if (pattern[j] === '*') {
// '*' can match zero or more characters
result = match(i, j + 1) || match(i + 1, j);
} else {
// Regular character must match
result = (text[i] === pattern[j]) && match(i + 1, j + 1);
}
// Store the result in the memoization map
memo.set(key, result);
return result;
}
return match(0, 0);
}
// Test cases
console.log(isMatchDP("report.pdf", "*.pdf")); // true
console.log(isMatchDP("document2023.docx", "document????.docx")); // true
console.log(isMatchDP("image.png", "*.jpg")); // false
console.log(isMatchGreedy("report.pdf", "*.pdf")); // true
console.log(isMatchGreedy("document2023.docx", "document????.docx")); // true
console.log(isMatchGreedy("image.png", "*.jpg")); // false
console.log(isMatchRecursive("report.pdf", "*.pdf")); // true
console.log(isMatchRecursive("document2023.docx", "document????.docx")); // true
console.log(isMatchRecursive("image.png", "*.jpg")); // false

Step-by-Step Explanation

Let's break down the implementation:

  1. Understand the Problem: First, understand that we need to implement a wildcard pattern matcher that supports '?' (matches any single character) and '*' (matches any sequence of characters).
  2. Identify Base Cases: Identify the base cases: if we've reached the end of the pattern, we should have also reached the end of the text for a match.
  3. Handle Special Characters: Handle the special characters '?' and '*' separately. The '*' character is particularly tricky as it can match zero or more characters.
  4. Implement Dynamic Programming Solution: Implement a bottom-up dynamic programming solution using a 2D table.
  5. Implement Greedy Solution: Implement a greedy solution with backtracking for better space efficiency.
  6. Implement Recursive Solution: Implement a recursive solution with memoization as an alternative approach.
ProblemSolutionCode
101 Logo
onenoughtone