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 - 1) 是“.” 或者p.charAt(i - 1) == s.charAt(j - 1), 那么我们有:

  match[i][j] = match[i - 1][j - 1];

如果p.chartAt(i - 1) 不是“.” 并且 p.charAt(i - 1) != s.charAt(j - 1), 那么我们有:

  match[i][j] = false;

好了,关键点来了,如果p.chartAt(i - 1) == ‘*’,那么怎么办呢?

  首先,如果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];

public class Solution {
public boolean isMatch(String s, String p) {
if (s == null || p == null) return false; while (p.length() >= && p.charAt() == '*') {
p = p.substring();
}
int row = p.length(), col = s.length();
boolean[][] match = new boolean[row + ][col + ];
match[][] = true;
for (int i = ; i <= row; i++) {
if (p.charAt(i - ) == '*') {
match[i][] = match[i - ][];
}
} for (int i = ; i <= row; i++) {
for (int j = ; j <= col; j++) {
if (p.charAt(i - ) == s.charAt(j - ) || p.charAt(i - ) == '.') {
match[i][j] = match[i - ][j - ];
} else if (p.charAt(i - ) == '*') {
if (p.charAt(i - ) == '.' || p.charAt(i - ) == s.charAt(j - )) {
match[i][j] = match[i - ][j - ] || match[i - ][j] || match[i][j - ] || match[i - ][j];
} else {
match[i][j] = match[i - ][j];
}
} else {
match[i][j] = false;
}
}
}
return match[row][col];
}
}

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 分析:
这题也是DP问题,横轴是S, 纵轴是P(含有?和*),那么我们可以得到:

  if (p.charAt(i - 1) == s.charAt(j - 1) || p.charAt(i - 1) == '?') {
    match[i][j] = match[i - 1][j - 1];
  } else if (p.charAt(i - 1) == '*') {
    match[i][j] = match[i - 1][j - 1] || match[i - 1][j] || match[i][j - 1];

    // match[i][j - 1] 指的是用* 替换S中1个j或多个j之前的character,当然那些character可以是连续的。
  }

 public class Solution {
public boolean isMatch(String s, String p) {
if (s == null || p == null) return false;
boolean[][] match = new boolean[p.length() + ][s.length() + ];
match[][] = true;
for (int i = ; i < match.length; i++) {
if (p.charAt(i - ) == '*') {
match[i][] = match[i - ][];
}
} for (int i = ; i < match.length; i++) {
for (int j = ; j < match[].length; j++) {
if (p.charAt(i - ) == s.charAt(j - ) || p.charAt(i - ) == '?') {
match[i][j] = match[i - ][j - ];
} else if (p.charAt(i - ) == '*') {
match[i][j] = match[i - ][j - ] || match[i - ][j] || match[i][j - ];
}
}
} return match[p.length()][s.length()];
}
}

Regular Expression Matching & Wildcard Matching的更多相关文章

  1. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

  2. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  3. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  4. 10. Regular Expression Matching字符串.*匹配

    [抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  5. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  6. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  7. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  8. No.010:Regular Expression Matching

    问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...

  9. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

随机推荐

  1. 20135337朱荟潼 Linux第二周学习总结——操作系统是如何工作的

    一.计算机是如何工作的--总结 三个法宝 存储程序计算机.函数调用堆栈.中断机制 二.堆栈 1.是c语言程序运行时必须的一个记录调用路径和参数的空间. 函数调用框架.传递参数.保存返回地址.提供局部变 ...

  2. 调研ios开发环境的演变

    一:ios的发展演变: 以下两句为百度百科IOS,可自行查阅,不多赘述,就Ctrl+c,Ctrl+v两句表示一下. 2007年1月9日苹果公司在Macworld展览会上公布,随后于同年的6月发布第一版 ...

  3. DataGridView显示行号-RowPostPaint

    DataGridView控件在显示数据时,我们有时候需要显示行号,以便检索查看方便使用. 但DataGridView默认没有设置显示行号的属性. 此时我们只要在DataGridView的RowPost ...

  4. DOM之节点类型加例子

    DOM= Document Object Model,文档对象模型,DOM可以以一种独立于平台和语言的方式访问和修改一个文档的内容和结构.换句话说,这是表示和处理一个HTML或XML文档的常用方法.D ...

  5. PHP + webuploader 视频上传

    上传方式,PHP默认方式 和 FTP 上传 1.修改PHP  配置: php.ini 修改以下配置项为适合的数据 php中 php.ini 文件修改 file_uploads = On //允许文件上 ...

  6. Win2008r2 设置 多用户同时远程

    Study From http://blog.sina.com.cn/s/blog_7ebe66420101tfln.html 1. 启动远程桌面,关闭防火墙 略过不提 2. 添加远程服务角色, 打开 ...

  7. Http建立连接的方式

    1.协议简介 Http 协议:应用层协议 TCP 协议:传输层协议,主要解决如何在IP层之上可靠的传递数据包,使在网络上的另一端收到发端发出的所有包,并且顺序与发出的顺序一致,TCP具有可靠,面向连接 ...

  8. oralce下载

    oracle的官网网址:oracle.com 打开之后选择中文 然后在页面的下方找到下载和试用链接 点击进入,选择数据库下载,在页面中找到如下部分点击进入即可下载对应版本的oracle

  9. MT【96】一道三角恒等变换题

    设$a,b,c$是正数,且$(a+b)(b+c)(c+a)=8$,证明不等式:$\frac{a+b+c}{3}≥[\frac{a^3+b^3+c^3}{3}]^{\frac{1}{27}}$ 评:记住 ...

  10. 生成器 yield

    由于生成器的其中一种创建方式与列表推导式很相似,这里先说一下列表推导式. 列表推导式 列表推导式又叫列表生成式,官方叫做 list comprehension.顾名思义,这个是用来生成列表的. 用法: ...