Wildcard Matching
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for “?” and “*”.
(通配符匹配)
“?” Matches any single character.
“*” Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
Note:
s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like “?” or “*”.
Example:
1. 动态规划
这个题跟之前的字符串匹配类似,不同点在于之前 “*” 可以匹配的是零个至多个连续的字符,而这道题目中的 “*” 是可以匹配任意的字符串。我们还是可以基于动态规划的思想来解决这道题目,具体解法如下。其中在 pattern 中第 j 个元素为 “*” 时,分为两种情况:
- 当前 “*” 表示为空串,则 dp[i][j] = dp[i-1][j]
- 当前 “*” 匹配当前的第 i 个字符 s[i-1], 则 dp[i][j] = dp[i][j-1]
1 | class Solution: |