Description
Given an input string (s) and a pattern §, 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 *.
Examples
Example 1:
Input: s = “aa” p = “a”
Output: false
Explanation: “a” does not match the entire string “aa”.
Example 2:
Input: s = “aa” p = “*”
Output: true
Explanation: ‘*’ matches any sequence.
Example 3:
Input: s = “cb” p = “?a”
Output: false
Explanation: ‘?’ matches ‘c’, but the second letter is ‘a’, which does not match ‘b’.
Example 4:
Input: s = “adceb” p = “*a*b”
Output: true
Explanation: The first ‘*’ matches the empty sequence, while the second ‘*’ matches the substring “dce”.
Example 5:
Input: s = “acdcb” p = “a*c?b”
Output: false
解题思路
看到这题第一个反应就是 #10 Regular Expression Matching(这个不知道为什么当时没写……现在按顺序下来就不补了)
那就是递归咯
前面是普通的特殊情况判断
后面是碰到 * 的情况
都比较好理解
class Solution {
public boolean isMatch(String s, String p) {
if(s.length() == 0){
while(p.length() > 0 && p.charAt(0) == '*')
p = p.substring(1);
}
if(s.length() == 0 && p.length() == 0)
return true;
if(s.length() == 0 || p.length() == 0){
return false;
}
if(p.charAt(0) == '?')
return isMatch(s.substring(1), p.substring(1));
if(p.charAt(0) == '*')
return isMatch(s.substring(1), p) || isMatch(s, p.substring(1));
return s.charAt(0) == p.charAt(0) && isMatch(s.substring(1), p.substring(1));
}
}
正确解法
嗯……上面那个Time Limited Exceeded了
“aaaabaaaabbbbaabbbaabbaababbabbaaaababaaabbbbbbaabbbabababbaaabaabaaaaaabbaabbbbaababbababaabbbaababbbba”
“*****b*aba***babaa*bbaba***a*aaba*b*aa**a*b**ba***a*a*”
看起来就是会超时的数据呢
下面说从discussion摸的正确答案
妙在那个 starIdx 和 match 上
举个例子
s = abababab
p = ab * ab
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | ↑ | |||||||
p | a | b | * | a | b | |||
point | ↑ |
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | ↑ | |||||||
p | a | b | * | a | b | |||
point | ↑ |
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | ↑(match) | |||||||
p | a | b | * | a | b | |||
point | ↑(starIdx) |
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | (match)↑ | |||||||
p | a | b | * | a | b | |||
point | (starIdx) | ↑ |
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | (match) | ↑ | ||||||
p | a | b | * | a | b | |||
point | (starIdx) | ↑ |
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | (match) | ↑ | ||||||
p | a | b | * | a | b | |||
point | (starIdx) | ↑ |
回溯
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | (match)↑ | |||||||
p | a | b | * | a | b | |||
point | (starIdx) | ↑ |
回溯
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | (match)↑ | |||||||
p | a | b | * | a | b | |||
point | (starIdx) | ↑ |
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | (match) | ↑ | ||||||
p | a | b | * | a | b | |||
point | (starIdx) | ↑ |
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | (match) | ↑ | ||||||
p | a | b | * | a | b | |||
point | (starIdx) | ↑ |
回溯
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | (match)↑ | |||||||
p | a | b | * | a | b | |||
point | (starIdx) | ↑ |
回溯
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | (match) | ↑ | ||||||
p | a | b | * | a | b | |||
point | (starIdx) | ↑ |
s | a | b | a | b | a | b | a | b |
---|---|---|---|---|---|---|---|---|
point | (match) | ↑ | ||||||
p | a | b | * | a | b | |||
point | (starIdx) | ↑ |
s | a | b | a | b | a | b | a | b | |
---|---|---|---|---|---|---|---|---|---|
point | (match) | ↑ | |||||||
p | a | b | * | a | b | ||||
point | (starIdx) | ↑ |
class Solution {
public boolean isMatch(String str, String pattern) {
int s = 0, p = 0, match = 0, starIdx = -1;
while (s < str.length()){
if (p < pattern.length() && (pattern.charAt(p) == '?' || str.charAt(s) == pattern.charAt(p))){
s++;
p++;
}
else if (p < pattern.length() && pattern.charAt(p) == '*'){
starIdx = p;
match = s;
p++;
}
else if (starIdx != -1){
p = starIdx + 1;
match++;
s = match;
}
else
return false;
}
while (p < pattern.length() && pattern.charAt(p) == '*')
p++;
return p == pattern.length();
}
}