Simple Pattern Matching Engine in JavaScript
This challenge asks you to build a basic pattern matching engine in JavaScript. Pattern matching is a fundamental concept in computer science used for searching and extracting data based on defined patterns, and it's crucial for tasks like data validation, text processing, and configuration parsing. Your engine should be able to determine if a given text string matches a provided pattern string, supporting a limited set of special characters for pattern definition.
Problem Description
You are to implement a function called matches(text, pattern) that takes two string arguments: text (the string to search within) and pattern (the pattern to match). The pattern string will support the following special characters:
.(dot): Matches any single character.*(asterisk): Matches zero or more occurrences of the preceding character.
The function should return true if the text string matches the pattern string, and false otherwise. The matching should occur from the beginning of the text string.
Key Requirements:
- The function must handle the special characters
.and*correctly. - The matching should be greedy with respect to the
*character (i.e., it should match as many characters as possible). - The function should be case-sensitive.
- The function should return
falseif the pattern is empty. - The function should return
trueif the text is empty and the pattern is also empty.
Expected Behavior:
The function should accurately determine whether the text matches the pattern based on the rules defined above. It should handle various combinations of characters, special characters, and edge cases.
Edge Cases to Consider:
- Empty text and empty pattern.
- Empty text and non-empty pattern.
- Non-empty text and empty pattern.
- Patterns with consecutive
*characters. - Patterns starting with
*. - Patterns containing only special characters.
- Text and patterns with Unicode characters.
Examples
Example 1:
Input: text = "baaabab", pattern = "ba*ab"
Output: true
Explanation: The pattern "ba*ab" matches the text "baaabab" because "ba" matches the beginning, "*" matches "aa", and "ab" matches the end.
Example 2:
Input: text = "baaabab", pattern = "a*ab"
Output: false
Explanation: The pattern "a*ab" does not match the text "baaabab" because the "a*" would consume the entire "baa" and leave "ab" to match the "ab" at the end, but the initial "b" doesn't match.
Example 3:
Input: text = "abc", pattern = "a.c"
Output: true
Explanation: The pattern "a.c" matches the text "abc" because "a" matches the first character, "." matches the second character, and "c" matches the third character.
Example 4:
Input: text = "abbbc", pattern = "ab*c"
Output: true
Explanation: The pattern "ab*c" matches the text "abbbc" because "ab" matches the beginning, "*" matches "bb", and "c" matches the end.
Example 5:
Input: text = "acdcb", pattern = "a*c?b"
Output: false
Explanation: The '?' character is not supported.
Constraints
- The length of the
textstring will be between 0 and 1000 characters (inclusive). - The length of the
patternstring will be between 0 and 100 characters (inclusive). - Both
textandpatternwill consist of lowercase English letters and the special characters.and*. - Performance: The function should complete within 100ms for all valid inputs.
Notes
- Consider using recursion or dynamic programming to solve this problem efficiently.
- Think about how to handle the
*character and its impact on the matching process. - Start with a simple implementation and gradually add support for more complex patterns.
- The order of characters in the pattern is significant.
- The matching must start from the beginning of the text string.
- Do not implement any other special characters beyond '.' and '*'. The presence of any other character in the pattern should result in a
falsereturn.