Given an input string (s) and a pattern §, implement regular expression matching with support for ‘.’ and ‘*’.
'.' Matches any single character.
'*' Matches zero or more of the preceding element.
The matching should cover the entire input string (not partial).
Note:
s
could be empty and contains only lowercase letters a-z.
p
could be empty and contains only lowercase letters a-z, and characters like . or *.
Example 1:
Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example 2:
Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:
Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:
Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".
Example 5:
Input:
s = "mississippi"
p = "mis*is*p*."
Output: false
思路:
1、“a"对应"a”, 这种匹配不解释了
2、任意字母对应".", 这也是正则常见
3、0到多个相同字符x,对应"x*", 比起普通正则,这个地方多出来一个前缀x. x代表的是 相同的字符中取一个,比如"aaaab"对应是"ab"
4、""还有一个易于疏忽的地方就是它的"贪婪性"要有一个限度.比如"aaa"对应"aa", 代码逻辑不能一路贪婪到底
5、正则表达式如果期望着一个字符一个字符的匹配,是非常不现实的.而"匹配"这个问题,非 常容易转换成"匹配了一部分",整个匹配不匹配,要看"剩下的匹配"情况.这就很好的把 一个大的问题转换成了规模较小的问题:递归
6、确定了递归以后,使用java来实现这个问题,会遇到很多和c不一样的地方,因为java对字符 的控制不像c语言指针那么灵活charAt一定要确定某个位置存在才可以使用.
7、如果pattern是"x"类型的话,那么pattern每次要两个两个的减少.否则,就是一个一个 的减少. 无论怎样减少,都要保证pattern有那么多个.比如s.substring(n), 其中n 最大也就是s.length()
class Solution {
public boolean isMatch(String s, String p) {
if (p.length() == 0)
return s.length() == 0;
// length == 1 is the case that is easy to forget.
// as p is subtracted 2 each time, so if original
// p is odd, then finally it will face the length 1
if (p.length() == 1)
return (s.length() == 1) && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.');
// next char is not '*': must match current character
if (p.charAt(1) != '*') {
if (s.length() == 0)
return false;
else
return (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')
&& isMatch(s.substring(1), p.substring(1));
}else{
// next char is *
while (s.length() > 0 && (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));
}
}
}
Java
class Solution {
public boolean isMatch(String s, String p) {
char[] text = s.toCharArray();
char[] pattern = p.toCharArray();
boolean T[][] = new boolean[text.length + 1][pattern.length + 1];
T[0][0] = true;
// Deals with patterns like a* or a*b* or a*b*c*
for (int i = 1; i < T[0].length; i++) {
if (pattern[i-1] == '*') {
T[0][i] = T[0][i - 2];
}
}
for (int i = 1; i < T.length; i++) {
for (int j = 1; j < T[0].length; j++) {
if (pattern[j - 1] == '.' || pattern[j - 1] == text[i - 1]) {
T[i][j] = T[i - 1][j - 1];
} else if (pattern[j - 1] == '*') {
T[i][j] = T[i][j - 2];
if (pattern[j-2] == '.' || pattern[j - 2] == text[i - 1]) {
T[i][j] = T[i][j] | T[i - 1][j];
}
} else {
T[i][j] = false;
}
}
}
return T[text.length][pattern.length];
}
}
JS
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
return new RegExp('^'+p+'$').test(s);
};
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
const matches = new RegExp(p).exec(s);
return (matches !== null && matches[0].length === s.length)
};