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. opencv2 用imwrite 抽取并保存视频图像帧

    最近在写一个车辆检测程序,程序中需要获取图像帧,并保存为图片,且放到指定目录中去,我在网上查了很多发现都是opencv1的有关操作,没有opencv2的操作,我参考网上的例子,结合着用opencv2新 ...

  2. 我的IT未来

    关于IT这个行业,我是持欣赏态度的,毕竟发展很快,对人们生活的改变也很大,所以,作为一个从事IT的人士,我很自豪. 读了刘先生的文章,我发现以前自己做的太少了,虽然偶尔也会吐血写程序,但还是太少了.以 ...

  3. redisCluster数据持久化

    Redis的数据回写机制 Redis的数据回写机制分同步和异步两种, 同步回写即SAVE命令,主进程直接向磁盘回写数据.在数据大的情况下会导致系统假死很长时间,所以一般不是推荐的. 异步回写即BGSA ...

  4. DEP

    DEP(Data execution protect)数据执行保护,这个功能需要操作系统和硬件的共同支持才可以生效.DEP的原理就是在系统的内存页中设置了一个标志位,标示这个内存页的属性(可执行). ...

  5. Xshell登录Docker

    使用SSH协议登录即可,用户名为docker,密码为tcuser

  6. CentOS下安装Python3

    目录 CentOS下安装Python3 下载 解压 配置 gcc sudo权限 vim 编译 安装 添加软链接 pip安装出错,找不到SSL 安装virtualenv和virtualenvwrappe ...

  7. Centos中安装和配置vsftp简明教程

    一.vsftp安装篇 # 安装vsftpd yum -y install vsftpd # 启动 service vsftpd start # 开启启动 chkconfig vsftpd on 二.v ...

  8. Swap HDU - 2819 (有关矩阵的二分匹配)

    题意见大佬:https://www.cnblogs.com/gj-Acit/archive/2013/08/17/3265502.html 题目大意很明确,交换图的某些行或者是某些列(可以都换),使得 ...

  9. 使用Ubuntu的Crontab定时任务需要注意的地方

    Ubuntu使用crontab定时任务  网上有很多教程,现在记录下我遇到的一些问题,需要注意的地方: 1.定时任务的日志存放路径 网上的说法:cron的日志存放在 /var/log/cron 里面 ...

  10. ZOJ1100 Mondriaan's Dream

    题目链接:QAQ 大致题意:有一个m行n列的矩阵,用1*2的骨牌(可横放或竖放)完全覆盖,骨牌不能重叠,有多少种不同的覆盖的方法? Solution: \(n,m\le11\),肯定是不能暴力的,又类 ...