[LeetCode]Wildcard Matching

public class Solution {
    public boolean isMatch(String s, String p) {
        int length1 = s.length();
        int length2 = p.length();
        if (length1 == 0) {
            return p.replace("*", "").length() == 0;
        }
        boolean[] record = new boolean[length1 + 1];
        record[0] = true;
        boolean allstar = true;
        for (int i = 0; i < length2; i++) {
            if (allstar)
                allstar = p.charAt(i) == '*';
            for (int j = length1 - 1; j >= 0; j--) {
                if (p.charAt(i) != '*') {
                    if (p.charAt(i) == s.charAt(j) || p.charAt(i) == '?') {
                        record[j + 1] = record[j];
                    } else {
                        record[j + 1] = false;
                    }
                } else {
                    boolean flg = false;
                    for (int jj = j + 1; jj >= 0; jj --) {
                        if (record[jj]) {
                            flg = true;
                            break;
                        }
                    }
                    record[j + 1] = flg;
                }
            }
            record[0] = allstar;
        }
        return record[length1];
    }
}

 

posted @ 2016-01-10 15:52  Weizheng_Love_Coding  阅读(90)  评论(0编辑  收藏  举报