You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
1.0 KiB

package q10;
/**
* 回溯法 对于*字符,可以直接忽略模式串中这一部分,或者删除匹配串的第一个字符,前提是它能够匹配模式串当前位置字符,即 pattern[0]。如果两种操作中有任何一种使得剩下的字符串能匹配,那么初始时,匹配串和模式串就可以被匹配。
*/
public class Solution {
public boolean isMatch(String text, String pattern) {
if (pattern.isEmpty()) return text.isEmpty();
boolean firstMatch = (!text.isEmpty() &&
(pattern.charAt(0) == text.charAt(0) || pattern.charAt(0) == '.'));
if (pattern.length() >= 2 && pattern.charAt(1) == '*') {
return (isMatch(text, pattern.substring(2)) ||
(firstMatch && isMatch(text.substring(1), pattern)));
} else {
return firstMatch && isMatch(text.substring(1), pattern.substring(1));
}
}
public static void main(String[] args) {
System.out.println(new Solution().isMatch("aaa", "a*a"));
}
}