Regular Expression Matching
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). The function prototype should be: bool isMatch(const char *s, const char *p)
Example
isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true
如果我面试的时候没有复习到这道题,然后面试官又考了这道题,我一定会在给面试官感谢信里写两个字:fuck you! 这家伙明显是不想让我过的意思。 我花了不止一个小时来理解这题的意思。
1. ‘*‘ Matches zero or more of the preceding element.
意思不是说对于a*, 它只可以match a, aa, aaa, 无限多个a。它还可以match "" (空字符串)。卧槽,你想得到吗,你想得到吗,你真的想得到吗?你想不到吧!
2. ".*"意思是可以匹配任意字符串, 比如 “ddd”, "dda", "abc"。
3. 对于"a*b",它匹配“b”, 但是不匹配“a”.
4. 如果p开头为“*”,你得把它去除掉。
好了,如果你理解了上面部分,我们就可以用DP解决问题了。
我们首先创建一个二维数组来保存中间变量。
int m = p.length();
int n = s.length();
boolean[][] match = new boolean[m + 1][n + 1]; (p是横轴,s是纵轴)
match[i][j]表明对于p的前i - 1个字符,是否匹配s的前j - 1个字符。
这里分几种情况:
如果p.chartAt(i) 是“.” 或者p.charAt(i - 1) == s.charAt(j - 1), 那么我们有:
match[i][j] = match[i - 1][j - 1];
如果p.chartAt(i) 不是“.” 并且 p.charAt(i - 1) != s.charAt(j - 1), 那么我们有:
match[i][j] = false;
好了,关键点来了,如果p.chartAt(i) == ‘*’,那么怎么办呢?
首先,如果p.charAt(i - 2) == ‘.‘ || p.charAt(i - 2) == s.charAt(j - 1)
那么我们是不是可以取match[i - 1][j - 1] (因为p.charAt(i - 1) == s.charAt(j - 1)如果上面条件成立), 或者 match[i - 2][j] ("x*" 直接变成 “”), 或者match[i][j - 1] ("x*" 变成 “x*x”) || match[i - 1][j] ("x*"变成 “x”);
所以,我们有: match[i][j] = match[i - 1][j - 1] || match[i - 2][j] || match[i][j - 1] || match[i - 1][j];
如果p.charAt(i - 2) != s.charAt(j - 1), 我们就只有一种方法:
match[i][j] = match[i - 2][j];
1 public class Solution { 2 /** 3 * @param s: A string 4 * @param p: A string includes "." and "*" 5 * @return: A boolean 6 */ 7 public boolean isMatch(String s, String p) { 8 if (s == null || p == null) return false; 9 10 while (p.length() >= 1 && p.charAt(0) == ‘*‘) { 11 p = p.substring(1); 12 } 13 int m = p.length(); 14 int n = s.length(); 15 boolean[][] match = new boolean[m + 1][n + 1]; 16 match[0][0] = true; 17 for (int i = 1; i <= m; i++) { 18 if (p.charAt(i - 1) == ‘*‘) { 19 match[i][0] = match[i - 2][0]; 20 } 21 } 22 23 for (int i = 1; i <= m; i++) { 24 for (int j = 1; j <= n; j++) { 25 if (p.charAt(i - 1) == s.charAt(j - 1) || p.charAt(i - 1) == ‘.‘) { 26 match[i][j] = match[i - 1][j - 1]; 27 } else if (p.charAt(i - 1) == ‘*‘) { 28 if (p.charAt(i - 2) == ‘.‘ || p.charAt(i - 2) == s.charAt(j - 1)) { 29 match[i][j] = match[i - 1][j - 1] || match[i - 2][j] || match[i][j - 1] || match[i - 1][j]; 30 } else { 31 match[i][j] = match[i - 2][j]; 32 } 33 } else { 34 match[i][j] = false; 35 } 36 } 37 } 38 return match[m][n]; 39 } 40 41 }
Wildcard Matching
Implement wildcard pattern matching with support for ‘?‘
and ‘*‘
.
‘?‘
Matches any single character.‘*‘
Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
Have you met this question in a real interview?
Yes
Example
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
1 public class Solution { 2 /** 3 * @param s: A string 4 * @param p: A string includes "?" and "*" 5 * @return: A boolean 6 */ 7 public boolean isMatch(String s, String p) { 8 if (s == null || p == null) 9 return false; 10 boolean[][] match = new boolean[p.length() + 1][s.length() + 1]; 11 match[0][0] = true; 12 for (int i = 1; i < match[0].length; i++) { 13 match[0][i] = false; 14 } 15 16 for (int i = 1; i < match.length; i++) { 17 if (p.charAt(i - 1) == ‘*‘) { 18 match[i][0] = match[i - 1][0]; 19 } 20 } 21 22 for (int i = 1; i < match.length; i++) { 23 for (int j = 1; j < match[0].length; j++) { 24 if (p.charAt(i - 1) == s.charAt(j - 1) || p.charAt(i - 1) == ‘?‘) { 25 match[i][j] = match[i - 1][j - 1]; 26 } else if (p.charAt(i - 1) == ‘*‘) { 27 match[i][j] = match[i - 1][j - 1] || match[i - 1][j] || match[i][j - 1]; 28 } 29 } 30 } 31 32 return match[p.length()][s.length()]; 33 } 34 }