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 ...
随机推荐
- 睡眠猴子——Alpha阶段项目总结
设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 针对广大失眠人群,尤其是程序猿用户的失眠问题.定义的清楚.描述过. 2. 是否有充足的时间来做计划 ...
- 第二章:蓝色巨人 IBM公司
COBOL(面向商业的通用语言) DEC(数字设备公司)华生实验室 造成失败的原因: 1,没有经营终端消费型产品 2,技术的流失 3.没有在意对手 成为的方法:合并,保守,谨慎 研究员的工作: 1,发 ...
- 《当大数据遇见网络:大数据与SDN》
总体结构: <当大数据遇见网络:大数据与SDN> 摘要 大数据和SDN无论是对于学术界还是工业界来说都极具吸引力.传统上人们都是分别在最前沿工作中研究这两个重要的领域.然而一方面,SDN的 ...
- Linux命令(六) 查看文件 cat tac more less tail
如果要查看文件,使用 cat less tac tail 和 more 中的任意一个即可. 1.cat 使用 cat 命令查看文件时会显示整个文件的内容,注意cat只能查看文本文件的内容,如 ...
- IIS 下 搭建简单的FTP服务器
1. 修改用户策略, 创建简单用户密码 命令行输入 gpedit.msc 打开组策略 位置 2. 创建一个FTP使用的用户 net user zhaobsh Test6530 /add 3. 安装II ...
- postman 学习网址
postman使用详解: http://gold.xitu.io/entry/57597a62a341310061337885 https://www.getpostman.com/docs/writ ...
- nginx mp3
location /mp3 { alias "d:/mp3"; default_type audio/mpeg; autoindex on; }
- 在PE32位下安装64位2003、2008系统
步骤 1.进PE(这里是老毛桃为例) 2.将系统(这里windows2008 r2 为例)拷到本地硬盘 3.将ios镜像出来 4.在PE桌面找到 “windows系统安装”,浏览 系统文件 \sou ...
- MT【187】余弦的线性组合
已知$\alpha+\beta+\gamma=\pi,(\alpha,\beta,\gamma\ge0)$ 求:$3\cos\alpha+4\cos\beta+5\cos\gamma$的最大值____ ...
- MT【88】抽象函数
分析:此类题一般有两种做法,第一种按解答题做法, 第二种作为填空题找对应的特殊函数,比如这里可以根据三角里和差化积得出$f(x)=\frac{1}{2}cos(\frac{\pi}{3}x)$