您的位置:首页 > 其它

LeetCode - Wildcard Matching

2015-12-25 12:15 344 查看
题目:

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

思路:
这题其实比Regular Expression Matching那道题容易,判断p中"*"的情况更简单。


package dp;

public class WildcardMatching {

public boolean isMatch(String s, String p) {
int slen = s.length();
int plen = p.length();
boolean[][] dp = new boolean[slen + 1][plen + 1];
dp[0][0] = true;
for (int i = 1; i <= plen; ++i)
dp[0][i] = p.charAt(i - 1) == '*' ? dp[0][i - 1] : false;
for (int i = 1; i <= slen; ++i)
dp[i][0] = false;
for (int i = 1; i <= slen; ++i) {
for (int j = 1; j <= plen; ++j) {
if (p.charAt(j - 1) == '*') {
dp[i][j] = dp[i - 1][j - 1] || dp[i - 1][j] || dp[i][j - 1];
} else {
dp[i][j] = match(s.charAt(i - 1), p.charAt(j - 1)) && dp[i - 1][j - 1];
}
}
}

return dp[slen][plen];
}

private boolean match(char a, char b) {
if (b == '?' || b == '*')
return true;
return a == b;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
WildcardMatching w = new WildcardMatching();
System.out.println(w.isMatch("aa","a"));
System.out.println(w.isMatch("aa","aa"));
System.out.println(w.isMatch("aaa","aa"));
System.out.println(w.isMatch("aa", "*"));
System.out.println(w.isMatch("aa", "a*"));
System.out.println(w.isMatch("ab", "?*"));
System.out.println(w.isMatch("aab", "c*a*b"));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: