LeetCode_44---Wildcard Matching

来源:互联网 发布:软件服务商 编辑:程序博客网 时间:2024/06/11 01:46

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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → false

Hide Tags
 Dynamic Programming Backtracking Greedy String
本题关键的一个考点:判断p字符串中的总长字符减去*号数,得到的总长是否长于s字符串,如果长过,那么就返回假。




package From41;/** * @author MohnSnow * @time 2015年6月19日 下午1:46:54 * @title Dynamic Programming Backtracking Greedy String *        '?' 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 * @translate 实现一个通配符算法---类似于正则表达式 * @refer s.matches(p); * @caution 边界条件太多, * @test "abefcdgiescdfimde", "ab*cd?i*de"不知道在哪儿停止匹配 */public class LeetCode44 {/** * @param argsmengdx *            -fnst *///递归--backtracking---毫无疑问会出错----Line 9: java.lang.StackOverflowErrorpublic static boolean isMatch(String s, String p) {if (s == "" && p == "") {return true;}if (s.length() == 0 || p.length() == 0) {return false;}if (s.equals(p)) {return true;} else {if (s.charAt(0) == p.charAt(0)) {return isMatch(s.substring(1, s.length()), p.substring(1, p.length()));} else {if (p.charAt(0) == '?') {return isMatch(s.substring(1, s.length()), p.substring(1, p.length()));} else if (p.charAt(0) == '*') {if (s.length() == 1) {return true;} else {return isMatch(s.substring(1, s.length()), p.substring(0, p.length()));}} else {return false;}}}}//优化了递归,但需要考虑的特殊情况太多,所以出错很多public static boolean isMatch1(String s, String p) {if (p.replace("*", "").length() > s.length())return false;if (s.equals("") && p.equals("") || (s.equals("") && p.equals("*"))) {return true;}if (s.length() == 0 || p.length() == 0) {return false;}if (s.equals(p)) {return true;} else {int sNode = 0, pNode = 0;for (; sNode < s.length() && pNode < p.length(); sNode++, pNode++) {if (s.charAt(sNode) == p.charAt(pNode) || p.charAt(pNode) == '?') {continue;} else {if (p.charAt(pNode) == '*') {if (s.length() == sNode + 1) {while (pNode < p.length()) {if (p.charAt(pNode) == '*') {System.out.println("匹配pNode: " + pNode);pNode++;} else {return false;}}if (pNode == p.length()) {return true;}} else {pNode--;continue;}} else {return false;}}}System.out.println("匹配pNode: " + pNode + " sNode: " + sNode);if (sNode >= s.length() && pNode < p.length()) {while (pNode < p.length()) {if (p.charAt(pNode) == '*') {System.out.println("匹配pNode: " + pNode);pNode++;} else {return false;}}if (pNode == p.length()) {return true;}} else if (sNode < s.length() && pNode >= p.length()) {return false;}else if (sNode >= s.length() && pNode >= p.length()) {return true;} else {return false;}}return false;}//动态规划---492msACpublic static boolean isMatch2(String s, String p) {if (p.replace("*", "").length() > s.length())return false;boolean[] d = new boolean[s.length() + 1];d[0] = true;for (int i = 1; i < s.length(); ++i) {d[i] = false;}for (int i = 1; i <= p.length(); ++i) {char pchar = p.charAt(i - 1);if (pchar == '*') {for (int j = 1; j <= s.length(); ++j) {d[j] = d[j - 1] || d[j];}}else {for (int j = s.length(); j >= 1; --j) {d[j] = d[j - 1] && (pchar == '?' || pchar == s.charAt(j - 1));}}d[0] = d[0] && pchar == '*';}return d[s.length()];}public static void main(String[] args) {String s = "abefcdgiescdfimde";String p = "ab*cd?i*de";System.out.println("匹配s: " + s + " p: " + p + " " + isMatch(s, p));System.out.println("匹配s: " + s + " p: " + p + " " + isMatch1(s, p));System.out.println("匹配s: " + s + " p: " + p + " " + isMatch2(s, p));}}



0 0