- Published on
ARTS 第43周
- Authors
- Name
- Jason Yang
- @yangjinlong86
Algorithm
10.正则表达式匹配
package org.nocoder.leetcode.solution;
/**
* 10. 正则表达式匹配
* https://leetcode-cn.com/problems/regular-expression-matching/
*/
public class RegularExpressionMatching {
public static boolean isMatch(String text, String pattern) {
if (pattern.isEmpty()) {
return text.isEmpty();
}
boolean first_match = (!text.isEmpty() && (pattern.charAt(0) == text.charAt(0) || pattern.charAt(0) == '.'));
if (pattern.length() >= 2 && pattern.charAt(1) == '*') {
return (isMatch(text, pattern.substring(2)) || (first_match && isMatch(text.substring(1), pattern)));
} else {
return first_match && isMatch(text.substring(1), pattern.substring(1));
}
}
public static void main(String[] args) {
System.out.println(isMatch("aa", "a*"));
}
}