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).
The function prototype should be:
bool isMatch(const char *s, const char *p)
Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
public class Solution {
public boolean isMatch(String str, String pattern) {
int s = 0;
int p = 0;
int index = -1;
int match = 0;
while (s < str.length()) {
if (p<pattern.length() && (str.charAt(s)==pattern.charAt(p)||pattern.charAt(p)=='?')) {
s++;
p++;
} else if (p<pattern.length() && pattern.charAt(p)=='*') {
index = p;
match = s;
p++;
} else if (index != -1) {
p = index+1;
match++;
s = match;
} else {
return false;
}
}
while (p<pattern.length() && pattern.charAt(p)=='*') {
p++;
}
return p == pattern.length();
}
}
本文详细介绍了如何实现一个通配符匹配算法,该算法支持使用 '?' 匹配任意单个字符和 '*' 匹配任意字符序列。通过函数原型 `bool isMatch(const char*s, const char*p)` 实现了输入字符串与模式字符串之间的完全匹配检查。
1376

被折叠的 条评论
为什么被折叠?



