Below is the implementation of the email filter system:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495function isMatch(text, pattern) { // Create DP table const m = text.length; const n = pattern.length; const dp = Array(m + 1).fill().map(() => Array(n + 1).fill(false)); // Empty text matches empty pattern dp[0][0] = true; // Empty text can match pattern 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] === '*') { // Match zero characters or one or more characters dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; } else if (pattern[j - 1] === '?' || pattern[j - 1] === text[i - 1]) { // Match single character dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = false; } } } return dp[m][n];} // Alternative recursive approach with memoizationfunction isMatchRecursive(text, pattern) { // Memoization cache const memo = new Map(); function match(i, j) { // Create a unique key for the current state const key = i + ',' + j; // If we've already computed this state, return the cached result if (memo.has(key)) { return memo.get(key); } // Base cases if (j === pattern.length) { return i === text.length; } 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] === '*') { // Match zero characters result = match(i, j + 1); // If not matched, try matching one or more characters if (!result) { result = match(i + 1, j); } } else if (pattern[j] === '?' || pattern[j] === text[i]) { // Match single character result = match(i + 1, j + 1); } else { result = false; } // Cache the result memo.set(key, result); return result; } return match(0, 0);} // Test casesconsole.log(isMatch("urgent-meeting", "urgent*")); // trueconsole.log(isMatch("quarterly-report", "*report")); // trueconsole.log(isMatch("budget-2023", "budget-??23")); // trueconsole.log(isMatch("team-meeting", "team-*ing")); // trueconsole.log(isMatch("project-update", "progress-*")); // false
Let's break down the implementation:
Implement the email filter system solution in different programming languages.
Below is the implementation of the email filter system in different programming languages. Select a language tab to view the corresponding code.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495function isMatch(text, pattern) { // Create DP table const m = text.length; const n = pattern.length; const dp = Array(m + 1).fill().map(() => Array(n + 1).fill(false)); // Empty text matches empty pattern dp[0][0] = true; // Empty text can match pattern 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] === '*') { // Match zero characters or one or more characters dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; } else if (pattern[j - 1] === '?' || pattern[j - 1] === text[i - 1]) { // Match single character dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = false; } } } return dp[m][n];} // Alternative recursive approach with memoizationfunction isMatchRecursive(text, pattern) { // Memoization cache const memo = new Map(); function match(i, j) { // Create a unique key for the current state const key = i + ',' + j; // If we've already computed this state, return the cached result if (memo.has(key)) { return memo.get(key); } // Base cases if (j === pattern.length) { return i === text.length; } 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] === '*') { // Match zero characters result = match(i, j + 1); // If not matched, try matching one or more characters if (!result) { result = match(i + 1, j); } } else if (pattern[j] === '?' || pattern[j] === text[i]) { // Match single character result = match(i + 1, j + 1); } else { result = false; } // Cache the result memo.set(key, result); return result; } return match(0, 0);} // Test casesconsole.log(isMatch("urgent-meeting", "urgent*")); // trueconsole.log(isMatch("quarterly-report", "*report")); // trueconsole.log(isMatch("budget-2023", "budget-??23")); // trueconsole.log(isMatch("team-meeting", "team-*ing")); // trueconsole.log(isMatch("project-update", "progress-*")); // false
Initialize a 2D boolean array dp[m+1][n+1] where m is the length of the text and n is the length of the pattern.
Set dp[0][0] = true (empty text matches empty pattern) and handle the case where the pattern starts with '*'.
Iterate through the text and pattern, filling the table based on the current characters.
When encountering '*', consider both matching zero characters and matching one or more characters.
When encountering '?', it matches any single character, so check the previous state dp[i-1][j-1].
For regular characters, they must match exactly. If they do, check the previous state dp[i-1][j-1].
The value at dp[m][n] indicates whether the entire text matches the entire pattern.
Array(m + 1).fill().map(() => Array(n + 1).fill(false))
to create a 2D arrayMap
for memoization in the recursive approach[[False] * (n + 1) for _ in range(m + 1)]
to create a 2D arrayor
operator for logical OR in the DP table fillingboolean[][] dp = new boolean[m + 1][n + 1]
to create a 2D arrayHashMap
for memoization in the recursive approachcharAt()
to access characters in stringsstd::vector>
to create a 2D arraystd::unordered_map
for memoization in the recursive approachstd::function
for a recursive lambda functionModify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the email filter system problem using a different approach than shown above.
An empty text matches an empty pattern.
An empty text can match a pattern consisting only of '*' wildcards.
A non-empty text cannot match an empty pattern.
Below is the implementation of the email filter system:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495function isMatch(text, pattern) { // Create DP table const m = text.length; const n = pattern.length; const dp = Array(m + 1).fill().map(() => Array(n + 1).fill(false)); // Empty text matches empty pattern dp[0][0] = true; // Empty text can match pattern 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] === '*') { // Match zero characters or one or more characters dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; } else if (pattern[j - 1] === '?' || pattern[j - 1] === text[i - 1]) { // Match single character dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = false; } } } return dp[m][n];} // Alternative recursive approach with memoizationfunction isMatchRecursive(text, pattern) { // Memoization cache const memo = new Map(); function match(i, j) { // Create a unique key for the current state const key = i + ',' + j; // If we've already computed this state, return the cached result if (memo.has(key)) { return memo.get(key); } // Base cases if (j === pattern.length) { return i === text.length; } 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] === '*') { // Match zero characters result = match(i, j + 1); // If not matched, try matching one or more characters if (!result) { result = match(i + 1, j); } } else if (pattern[j] === '?' || pattern[j] === text[i]) { // Match single character result = match(i + 1, j + 1); } else { result = false; } // Cache the result memo.set(key, result); return result; } return match(0, 0);} // Test casesconsole.log(isMatch("urgent-meeting", "urgent*")); // trueconsole.log(isMatch("quarterly-report", "*report")); // trueconsole.log(isMatch("budget-2023", "budget-??23")); // trueconsole.log(isMatch("team-meeting", "team-*ing")); // trueconsole.log(isMatch("project-update", "progress-*")); // false
Let's break down the implementation:
Implement the email filter system solution in different programming languages.
Below is the implementation of the email filter system in different programming languages. Select a language tab to view the corresponding code.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495function isMatch(text, pattern) { // Create DP table const m = text.length; const n = pattern.length; const dp = Array(m + 1).fill().map(() => Array(n + 1).fill(false)); // Empty text matches empty pattern dp[0][0] = true; // Empty text can match pattern 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] === '*') { // Match zero characters or one or more characters dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; } else if (pattern[j - 1] === '?' || pattern[j - 1] === text[i - 1]) { // Match single character dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = false; } } } return dp[m][n];} // Alternative recursive approach with memoizationfunction isMatchRecursive(text, pattern) { // Memoization cache const memo = new Map(); function match(i, j) { // Create a unique key for the current state const key = i + ',' + j; // If we've already computed this state, return the cached result if (memo.has(key)) { return memo.get(key); } // Base cases if (j === pattern.length) { return i === text.length; } 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] === '*') { // Match zero characters result = match(i, j + 1); // If not matched, try matching one or more characters if (!result) { result = match(i + 1, j); } } else if (pattern[j] === '?' || pattern[j] === text[i]) { // Match single character result = match(i + 1, j + 1); } else { result = false; } // Cache the result memo.set(key, result); return result; } return match(0, 0);} // Test casesconsole.log(isMatch("urgent-meeting", "urgent*")); // trueconsole.log(isMatch("quarterly-report", "*report")); // trueconsole.log(isMatch("budget-2023", "budget-??23")); // trueconsole.log(isMatch("team-meeting", "team-*ing")); // trueconsole.log(isMatch("project-update", "progress-*")); // false
Initialize a 2D boolean array dp[m+1][n+1] where m is the length of the text and n is the length of the pattern.
Set dp[0][0] = true (empty text matches empty pattern) and handle the case where the pattern starts with '*'.
Iterate through the text and pattern, filling the table based on the current characters.
When encountering '*', consider both matching zero characters and matching one or more characters.
When encountering '?', it matches any single character, so check the previous state dp[i-1][j-1].
For regular characters, they must match exactly. If they do, check the previous state dp[i-1][j-1].
The value at dp[m][n] indicates whether the entire text matches the entire pattern.
Array(m + 1).fill().map(() => Array(n + 1).fill(false))
to create a 2D arrayMap
for memoization in the recursive approach[[False] * (n + 1) for _ in range(m + 1)]
to create a 2D arrayor
operator for logical OR in the DP table fillingboolean[][] dp = new boolean[m + 1][n + 1]
to create a 2D arrayHashMap
for memoization in the recursive approachcharAt()
to access characters in stringsstd::vector>
to create a 2D arraystd::unordered_map
for memoization in the recursive approachstd::function
for a recursive lambda functionModify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the email filter system problem using a different approach than shown above.
An empty text matches an empty pattern.
An empty text can match a pattern consisting only of '*' wildcards.
A non-empty text cannot match an empty pattern.