Regular Expression Matching & Wildcard Matching
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)
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
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).
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的更多相关文章
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 10. Regular Expression Matching字符串.*匹配
[抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- No.010:Regular Expression Matching
问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...
- Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- ElasticSearch 2 (34) - 信息聚合系列之多值排序
ElasticSearch 2 (34) - 信息聚合系列之多值排序 摘要 多值桶(terms.histogram 和 date_histogram)动态生成很多桶,Elasticsearch 是如何 ...
- ElasticSearch 2 (21) - 语言处理系列之单词识别
ElasticSearch 2 (21) - 语言处理系列之单词识别 摘要 一个英语单词相对容易识别:因为英语单词是被空格或(某些)标点符号隔开的.但在英语中也有反例:you're 这个词是一个单词还 ...
- ElasticSearch 2 (17) - 深入搜索系列之部分匹配
ElasticSearch 2 (17) - 深入搜索系列之部分匹配 摘要 到目前为止,我们介绍的所有查询都是基于完整术语的,为了匹配,最小的单元为单个术语,我们只能查找反向索引中存在的术语. 但是, ...
- 2017-08-20 block,inline和inline-block概念和区别
display:inline.block.inline-block的区别 display:block就是将元素显示为块级元素. block元素的特点是: 总是在新行上开始: 高度,行高以及顶和底边距都 ...
- 11th 如果重新来过
如果重新来过,我想我不会再因任何阻碍而选择中途放弃了.为了追求完美,结果做不到自己想要的程度,就备感挫败感,于是乎就求全责备,无法继续进行下去,即便你一直原地踏步,其实也就相当于是放弃了,跟不做没有什 ...
- jdk命令行工具:jstat与jmap
转自文章:http://blog.csdn.net/gzh0222/article/details/8538727 C:\Users\Administrator\Desktop>jstat -g ...
- [转帖]Docker里运行Docker docker in docker(dind)
Docker里运行Docker docker in docker(dind) http://www.wantchalk.com/c/devops/docker/2017/05/24/docker-in ...
- Jenkins配置匿名用户拥有只读权限
场景:查看cucumber reporting测试报告时需要登陆,比较麻烦 解决:允许匿名用户拥有只读权限 操作:Jenkins->系统管理->全局安全配置->授权策略,勾选“All ...
- C# 多线程初级汇总
异步委托 创建线程的一种简单方式是定义一个委托,并异步调用它 委托是方法的类型安全的引用 Delegate类还支持异步地调用方法.在后台,Delegate类会创建一个执行任务的线程 投票,并检查委托是 ...
- Military Problem CodeForces - 1006E(dfs搜一下 标记一下)
题意: 就是有一颗树 然后每次询问 父结点 的 第k个结点是不是他的子嗣...是的话就输出这个子嗣..不是 就输出-1 解析: 突然想到后缀数组的sa 和 x的用法..就是我们可以用一个id标记当前 ...