r""" Recursive matching algorithm. | Time complexity: O(2^(\|text\| + \|pattern\|)) | Space complexity: Recursion depth is O(\|text\| + \|pattern\|). :param text: Text to match. :param pattern: Pattern to match. :return: ``True`` if `text` matches `pattern`, ``False`` other
(text: str, pattern: str)
| 11 | |
| 12 | |
| 13 | def recursive_match(text: str, pattern: str) -> bool: |
| 14 | r""" |
| 15 | Recursive matching algorithm. |
| 16 | |
| 17 | | Time complexity: O(2^(\|text\| + \|pattern\|)) |
| 18 | | Space complexity: Recursion depth is O(\|text\| + \|pattern\|). |
| 19 | |
| 20 | :param text: Text to match. |
| 21 | :param pattern: Pattern to match. |
| 22 | :return: ``True`` if `text` matches `pattern`, ``False`` otherwise. |
| 23 | |
| 24 | >>> recursive_match('abc', 'a.c') |
| 25 | True |
| 26 | >>> recursive_match('abc', 'af*.c') |
| 27 | True |
| 28 | >>> recursive_match('abc', 'a.c*') |
| 29 | True |
| 30 | >>> recursive_match('abc', 'a.c*d') |
| 31 | False |
| 32 | >>> recursive_match('aa', '.*') |
| 33 | True |
| 34 | """ |
| 35 | if not pattern: |
| 36 | return not text |
| 37 | |
| 38 | if not text: |
| 39 | return pattern[-1] == "*" and recursive_match(text, pattern[:-2]) |
| 40 | |
| 41 | if text[-1] == pattern[-1] or pattern[-1] == ".": |
| 42 | return recursive_match(text[:-1], pattern[:-1]) |
| 43 | |
| 44 | if pattern[-1] == "*": |
| 45 | return recursive_match(text[:-1], pattern) or recursive_match( |
| 46 | text, pattern[:-2] |
| 47 | ) |
| 48 | |
| 49 | return False |
| 50 | |
| 51 | |
| 52 | def dp_match(text: str, pattern: str) -> bool: |
nothing calls this directly
no outgoing calls
no test coverage detected