Transaction Description Pattern Matcher
Stripe processes millions of financial transactions daily. To ensure compliance, detect fraud, and categorize transactions, we often need to identify specific patterns within transaction descriptions. These patterns can sometimes be flexible, including wildcards to account for variations in merchant naming or product codes. Efficiently matching these patterns against large volumes of descriptions is critical for real-time processing and analysis.
Implement a function matchTransactionPattern that determines if a given transactionDescription matches a pattern containing special wildcard characters.
The wildcard characters are:
?: Matches any single character.*: Matches any sequence of zero or more characters.The match must cover the entire transactionDescription.
function matchTransactionPattern(transactionDescription: string, pattern: string): boolean
transactionDescription: A string representing the text to be matched. It consists of lowercase English letters.pattern: A string containing the pattern to match against. It can include lowercase English letters, and the wildcard characters ? and *.Returns true if the transactionDescription matches the pattern completely, false otherwise.
transactionDescription can match an empty pattern (e.g., "" matches "" is true).transactionDescription can match a pattern consisting only of * characters (e.g., "" matches "*" or "**" is true).pattern can only match an empty transactionDescription.Examples
Example 1
Input:
transactionDescription = "adceb", pattern = "a*b"
Output:
true
Note:
The 'a' matches 'a'. The '*' matches the substring 'dce'. The 'b' matches 'b'. The entire transaction description is matched.
Example 2
Input:
transactionDescription = "acdcb", pattern = "a*c?b"
Output:
false
Note:
Despite various ways to interpret the wildcards, the pattern a*c?b cannot fully match acdcb. For example, if '*' matches 'cd', then 'c?b' needs to match 'cb'. 'c' matches 'c', '?' matches 'b', but then the pattern 'b' remains with nothing left in the description.
Example 3
Input:
transactionDescription = "abcde", pattern = "ace"
Output:
true
Note:
The 'a' matches 'a'. The first '' matches 'b'. The 'c' matches 'c'. The second '' matches 'd'. The 'e' matches 'e'. The entire transaction description is matched.
Example 4
Input:
transactionDescription = "", pattern = "*"
Output:
true
Note:
An empty string can be matched by a '*' wildcard (matching zero characters).
Constraints
0 <= transactionDescription.length <= 20000 <= pattern.length <= 2000transactionDescription consists only of lowercase English letters.pattern consists only of lowercase English letters, ?, or *.+, {}, [], ^, $, etc.Loading editor...