2-D Dynamic Programming Two strings
565. Regular Expression Matching
You are given a string s and a pattern p. Return true if the pattern matches the whole of s, and
false otherwise.
The pattern holds lowercase letters and two special characters:
- A letter matches that same letter.
.matches any one letter.*matches zero or more copies of the character right before it. Soa*matches"","a","aa"and so on. In.*, each copy of.can be a different letter, so.*matches any string, the empty one included.
The match must cover all of s, from its first letter to its last. A pattern that matches only the start
of s does not count.
Example 1
Input: s = "bread", p = "b.*a."
trueExplanation: b matches the b, .* takes "re", a matches the a, and . takes the last letter, d.
Example 2
Input: s = "", p = "x*y*"
trueExplanation: Both x* and y* take zero copies, so the pattern matches the empty string.
Example 3
Input: s = "abb", p = "a*b"
falseExplanation: The pattern matches "ab", the start of s, but the last b is left over. A b with no * after it
matches exactly one b.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
0 ≤ s.length, p.length ≤ 2000sholds only lowercase English letters.pholds only lowercase English letters,.and*.- Every
*inpcomes right after a letter or a..
Clarifying questions
What you would ask an interviewer before writing any code.