需求
2.代码
java
public class RegularExpressionMatching {
public boolean isMatch(String s, String p) {
if (p.isEmpty()) {
return s.isEmpty();
}
boolean firstMatch =!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.');
if (p.length() >= 2 && p.charAt(1) == '*') {
return isMatch(s, p.substring(2)) || (firstMatch && isMatch(s.substring(1), p));
} else {
return firstMatch && isMatch(s.substring(1), p.substring(1));
}
}
public static void main(String[] args) {
RegularExpressionMatching rem = new RegularExpressionMatching();
String s = "aa";
String p = "a";
System.out.println(rem.isMatch(s, p)); // false
s = "aa";
p = "a*";
System.out.println(rem.isMatch(s, p)); // true
s = "ab";
p = ".*";
System.out.println(rem.isMatch(s, p)); // true
}
}