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 lettersa-z
.p
could be empty and contains only lowercase lettersa-z
, and characters like?
or*
.
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
根据题意 就是正则判断 与上面一道题目RegularExpressionMatching 类似
方法一遍历实现具体实现方法如下:(最终超时)
public boolean isMatch(String s, String p) {
if(s.isEmpty()) {
if(p.isEmpty()) {
return true;
}else {
if(p.charAt(0)=='*') {
return isMatch(s,p.substring(1));
}
}
return false;
}else if(p.isEmpty()) {
return false;
}
boolean first = (s.charAt(0) == p.charAt(0))||(p.charAt(0)=='?')||(p.charAt(0)=='*');
if(first) {
if(p.charAt(0)=='*'){
if(isMatch(s.substring(1),p.substring(1))) {
return true;
}
if(isMatch(s.substring(0),p.substring(1))) {
return true;
}
if(isMatch(s.substring(1),p)) {
return true;
}
}else {
return isMatch(s.substring(1),p.substring(1));
}
}
return false;
}
方法二:
public boolean isMatch(String s, String p) {
if(s.isEmpty()) {
if(p.isEmpty()) {
return true;
}else {
if(p.charAt(0)=='*') {
return isMatch(s,p.substring(1));
}
}
return false;
}else if(p.isEmpty()) {
return false;
}
boolean first = (s.charAt(0) == p.charAt(0))||(p.charAt(0)=='?')||(p.charAt(0)=='*');
if(first) {
if(p.charAt(0)=='*'){
int pIndex = 1;
char tmp = p.charAt(0);
while( pIndex<p.length() && (tmp=p.charAt(pIndex))=='*') {
pIndex++;
}
if(p.length() == pIndex) {
return true;
}
for(int sIndex=0;sIndex<s.length();sIndex++) {
if((s.charAt(sIndex)==tmp || tmp =='?' )&& isMatch(s.substring(sIndex),p.substring(pIndex))) {
return true;
}
}
}else {
return isMatch(s.substring(1),p.substring(1));
}
}
return false;
}
找到正则中的非*的数据在元数据中的位置 再进行判断
"abbabaaabbabbaababbabbbbbabbbabbbabaaaaababababbbabababaabbababaabbbbbbaaaabababbbaabbbbaabbbbababababbaabbaababaabbbababababbbbaaabbbbbabaaaabbababbbbaababaabbababbbbbababbbabaaaaaaaabbbbbaabaaababaaaabb" "**aa*****ba*a*bb**aa*ab****a*aaaaaa***a*aaaa**bbabb*b*b**aaaaaaaaa*a********ba*bbb***a*ba*bb*bb**a*b*bb"
好吧又超时了
方法三:
两条字符串 用动态规划进行实现判断,当 当前字符为*时候判断 dp[i][j]=dp[i-1][j] || dp[i][j-1] 当为相同或是?时候
dp[i][j]=dp[i-1][j-1] 否则dp[i][j]=false;
public boolean isMatch(String s, String p) {
boolean[][] res = new boolean[s.length()+1][p.length()+1];
res[0][0] = true;
for(int i=1;i<= p.length();i++) {
res[0][i] = p.charAt(i-1)=='*'?res[0][i-1]:false;
}
for(int sIndex=1;sIndex<=s.length();sIndex++) {
for(int pIndex=1;pIndex<=p.length();pIndex++) {
if(s.charAt(sIndex-1) == p.charAt(pIndex-1) || p.charAt(pIndex-1)=='?') {
res[sIndex][pIndex] = res[sIndex-1][pIndex-1];
}else if (p.charAt(pIndex - 1) == '*'){
res[sIndex][pIndex] = res[sIndex-1][pIndex]||res[sIndex][pIndex-1];
}
}
}
return res[s.length()][p.length()];
}