leetcode-10-正则表达式

来源:互联网 发布:移动数据加油包 编辑:程序博客网 时间:2024/05/22 19:57

abc      .*abc 考虑这种情况,是匹配的。即带*号的可以理解是为备用情况,只有当不使用.*时后面无法匹配才使用.*,比如这种情况abc      .*bc。

这里写图片描述

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

还有DP法。

0 0