[LeetCode]Regular Expression Matching

public class Solution {
    public boolean isMatch(String s, String p) {
        if (p.length() == 0) {
            return s.length() == 0;
        }
        if (p.length() == 1 || p.charAt(1) != '*') {
            if (s.length() != 0 && (s.charAt(0) ==  p.charAt(0) || p.charAt(0) == '.')) {
                return isMatch(s.substring(1), p.substring(1));
            } else {
                return false;
            }
        } else {
            while (s.length() != 0 && (s.charAt(0) ==  p.charAt(0) || p.charAt(0) == '.')) {
                if (isMatch(s, p.substring(2))) {
                    return true;
                }
                s = s.substring(1);
            }
            return isMatch(s, p.substring(2));
        }
    }
}

 

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