Full-String Matching with Dot and Star Wildcards
Dynamic ProgrammingAuto-graded · 23 test casesLast updated
Decide whether a whole text matches a pattern with '.' and '*' wildcards, using prefix-based dynamic programming over text and pattern positions.
Wildcard Pattern Matcher
You are given a text text and a pattern pattern. Write a function that decides whether the pattern matches the entire text. Matching a prefix or a substring is not enough.
The pattern is made of lowercase letters and two special symbols:
.stands for exactly one arbitrary character.*applies to the element immediately before it (a letter or.) and means that element may appear zero or more times in a row.
A pattern element followed by * is treated as a single unit. For example, a* can produce "", "a", "aa", and so on, and .* can produce any string, including the empty one.
Return True if the whole text can be produced by the pattern, otherwise False.
Malformed patterns
The input is guaranteed to give every * a valid preceding element. If a pattern ever violates this (it starts with *, or has two * in a row), return False, even when the text is empty.
Constraints
0 <= len(text) <= 200 <= len(pattern) <= 20textcontains only lowercase English letters.patterncontains only lowercase English letters,.and*.- Every
*inpatternis guaranteed to have a valid preceding element to repeat.
Examples
Example 1
Input: is_full_match('aa', 'a')
Output: False
Example 2
Input: is_full_match('aa', 'a*')
Output: True
Example 3
Input: is_full_match('ab', '.*')
Output: True
Hints
Hint 1
Think of the state as a pair (i, j): does the first i characters of the text match the first j characters of the pattern? Build a table over these pairs.
Hint 2
Treat `x*` as one unit. When the last pattern character is `*`, you either use zero copies of the preceding element (skip two pattern characters) or use one more copy to absorb the last text character (stay on the same pattern position, drop one text character).
Hint 3
An empty text can still match a pattern like `a*b*c*`. Fill the first row of the table by letting each `x*` pair collapse to nothing, and a greedy left-to-right scan will fail on cases like text `aaa` with pattern `a*a`.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Whether a match works out depends on two things at once: how much of the text has been consumed and how much of the pattern has been consumed. That suggests a table over prefixes.
Let dp[i][j] be True when the first i characters of the text are matched exactly by the first j characters of the pattern.
Setup
dp[0][0] = True: an empty pattern matches an empty text.- While scanning the pattern for the empty-text row, validate each
*. If it is at position 0 or follows another*, returnFalseimmediately. Otherwisedp[0][j] = dp[0][j-2], sincex*can vanish entirely. Any other kind of pattern character leavesdp[0][j] = False.
Transition for i >= 1, j >= 1, based on the last pattern character pattern[j-1]:
- Letter or
.: if it is.or equalstext[i-1], the two last characters pair up, sodp[i][j] = dp[i-1][j-1]. *: letx = pattern[j-2]. Either- use zero copies of
x:dp[i][j-2], or - use at least one copy, which requires
xto be.or equal totext[i-1]; the last copy consumestext[i-1]and the samex*can still cover the rest:dp[i-1][j].
- use zero copies of
- A letter that does not match: the cell stays
False.
The answer is dp[m][n].
Why it is correct
If the last pattern token is a single character, it must consume exactly one text character, the last one, so the earlier prefixes must match each other. If the last token is x*, a match uses either zero copies of x or at least one. With zero copies the text must match the pattern minus x*. With at least one, the final copy takes the last text character, and what remains must match the same pattern prefix again, because x* may still produce further copies. Each case also builds a valid match in reverse, so the recurrence captures exactly the real matches. Every dependency sits in an earlier row or an earlier column, so row-by-row, left-to-right filling is valid.
The step dp[i-1][j] avoids enumerating how many copies x* should take, so each cell costs constant time.
Complexity
- Time: O(m * n), with m the text length and n the pattern length.
- Space: O(m * n) for the table; it can be reduced to O(n) using two rolling rows.
Reference solution
def is_full_match(text, pattern):
m, n = len(text), len(pattern)
# dp[i][j] is True when text[:i] is matched exactly by pattern[:j]
dp = [[False] * (n + 1) for _ in range(m + 1)]
dp[0][0] = True
# Empty text versus each pattern prefix; also rejects malformed stars.
for j in range(1, n + 1):
if pattern[j - 1] == '*':
if j < 2 or pattern[j - 2] == '*':
return False
dp[0][j] = dp[0][j - 2]
for i in range(1, m + 1):
for j in range(1, n + 1):
token = pattern[j - 1]
if token == '.' or token == text[i - 1]:
dp[i][j] = dp[i - 1][j - 1]
elif token == '*':
if j < 2 or pattern[j - 2] == '*':
return False
# zero copies of the preceding element
dp[i][j] = dp[i][j - 2]
prev = pattern[j - 2]
# one more copy absorbing text[i-1]
if prev == '.' or prev == text[i - 1]:
dp[i][j] = dp[i][j] or dp[i - 1][j]
return dp[m][n]