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 ...
随机推荐
- 第九周PSP&进度条
PSP 一.表格: D日期 C类型 C内容 S开始时间 E结束时间 I时间间隔 T净时间(mins) 预计花费时间(mins) 11月11号 讨论 讨论beta发布 09:00 09:54 1 ...
- Python爬虫利器:BeautifulSoup库
Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. BeautifulSoup ...
- html+css照片墙
html文件 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF- ...
- [国家集训队]middle
[国家集训队]middle 题目 解法 开\(n\)颗线段树,将第\(i\)颗线段树中大于等于第\(i\)小的数权值赋为1,其他的则为-1,对于每个区间维护一个区间和,最大前缀和,最大后缀和. 然后二 ...
- 【设计模式】—— 命令模式Commond
前言:[模式总览]——————————by xingoo 模式意图 将一个请求封装成一个对象,从而对这个命令执行撤销.重做等操作. 典型的Eclipse开发中,编辑器的操作就需要用到这个模式,比如Un ...
- linux命令行打包、压缩及解压缩
使用命令: tar 打包: tar -zcvf 目标文件 源文件或文件夹 目标文件为要打包成的文件的文件名, 打包后文件的 格式取决于目标文件的后缀名 单文件或文件夹打包 tar -zcvf ind ...
- BZOJ5418 NOI2018屠龙勇士(excrt)
显然multiset求出每次用哪把剑.注意到除了p=1的情况,其他数据都保证了ai<pi,于是先特判一下p=1.比较坑的是还可能存在ai=pi,稍微考虑一下. 剩下的部分即解bix≡ai(mod ...
- gdb调试coredump文件
linux上程序崩溃起来挺烦人,不过linux 比较好的是有gdb. 1.生成coredump文件 echo "ulimit -c unlimited" >> /etc ...
- 【题解】 bzoj2460: [BeiJing2011]元素 (线性基)
bzoj2460,戳我戳我 Solution: 线性基板子,没啥好说的,注意long long 就好了 Code: //It is coded by Ning_Mew on 5.29 #include ...
- 【转】如何应用Query语句进行规则的语法设置?
在Altium Designer中, 设计规则通常用来定义用户的设计需求. 这些规则涵盖了设计的方方面面, 从布线宽度, 对象的安全间距,内电层的连接风格, 过孔风格等等. 设计规则不仅能在PCB设计 ...