LeetCode解题报告—— Regular Expression Matching
Given an input string (s
) and a pattern (p
), 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).
Note:
s
could be empty and contains only lowercase lettersa-z
.p
could be empty and contains only lowercase lettersa-z
, and characters like.
or*
.
Example 1:
Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".
Example 2:
Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
Example 3:
Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".
Example 4:
Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches "aab".
Example 5:
Input:
s = "mississippi"
p = "mis*is*p*."
Output: false
分析:没什么思路,还是看了一下discussion,解法是用dp,感觉hard的题目大部分解法是 DP,BFS和DFS这几种。这题使用二维的dp数组来解决,有点类似求公共最长字串的那个dp。本题要明白的是,字符串和模式串的匹配是从左往右进行,可以分解为重复的子问题,比如例子5中的 s 就可以根据索引来分解为 m, mi, mis, miss.......等字串,然后p也可以分局索引来分解成字串,这样构成了dp数组dp[i][j],表示从 0~i 的子串和从0~j的子模式串是否匹配,那么么最后要求的结果是 dp[s.length()][p.length()]。
dp的要点之一是从低向上计算,后面的计算会用到前面的计算结果,所以理清楚前后的计算关系是很重要的,对于这题来说(考虑到从左往右匹配过程的一般位置 i, j),情况如下
1, If p.charAt(j) == s.charAt(i) : dp[i][j] = dp[i-1][j-1];
2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1];
3, If p.charAt(j) == '*':
here are two sub conditions:
1 if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2] //in this case, a* only counts as empty
2 if p.charAt(i-1) == s.charAt(i) or p.charAt(i-1) == '.':
dp[i][j] = dp[i-1][j] //in this case, a* counts as multiple a
or dp[i][j] = dp[i][j-1] // in this case, a* counts as single a
or dp[i][j] = dp[i][j-2] // in this case, a* counts as empty
代码:
class Solution {
public boolean isMatch(String s, String p) {
if(s==null || p==null) return false;
boolean[][] dp=new boolean[s.length()+1][p.length()+1]; dp[0][0]=true;
for(int i=0;i<p.length();i++){
if(p.charAt(i)=='*' && dp[0][i-1]==true){ // 当i=0时,p.charAt(0)不会是*,当i=1时,比如p为a*,正好匹配s为空时的情况。dp[0][1]一定是false
dp[0][i+1]=true;
}
} for(int i=0;i<s.length();i++){
for(int j=0;j<p.length();j++){
if(s.charAt(i)==p.charAt(j) || p.charAt(j)=='.'){
dp[i+1][j+1]=dp[i][j];
}else if(p.charAt(j)=='*'){
if(s.charAt(i)!=p.charAt(j-1) && p.charAt(j-1)!='.'){
dp[i+1][j+1]=dp[i+1][j-1];
}else{
dp[i+1][j+1]= dp[i][j+1] || dp[i+1][j-1]; // a* 匹配掉 s 中的一个a,也可以不匹配
}
}
}
}
return dp[s.length()][p.length()];
}
}
对于dp,要注意的的是对于每个 dp[i][j] 都应该将其作为一个单独问题考虑才对,不要像递归或者回溯那样考虑其在整个问题中和其它子问题的前后依赖关系或计算过程等,这样有助于理清思路。
还要一个是上面的标记的注意点,如果 p.charAt(j-1) != s.charAt(i) && p.charAt(j-1) != '.',这种条件下 a* 这样的模式不能匹配s.charAt(i),那么dp[i+1][j+1] 的选择只能是dp[i+1][j-1],也就是 a 出现的次数是0个,那么i+1 索引是不动的,而模式串要舍弃掉后两位。如果能匹配上,注意的是,这时候有2个选择,可以匹配一个,也可以选择不匹配。
LeetCode解题报告—— Regular Expression Matching的更多相关文章
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- LeetCode OJ:Regular Expression Matching(正则表达式匹配)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【LeetCode】010. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【LeetCode】10.Regular Expression Matching(dp)
[题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
随机推荐
- bzoj1878: [SDOI2009]HH的项链(主席树/离线+BIT)
这题有离线和在线两种做法. 离线:将查询区间按左端点排序,预处理出所有数下一次的出现位置,一开始将所有第一次出现的数a[i]++,之后当扫到这个数的时候a[next[i]]++,相当于差分,给之后 ...
- IOI2000 Post Office (POJ1160)
前言 昨天XY讲课!讲到这题!还是IOI的题!不过据说00年的时候DP还不流行. 题面 http://poj.org/problem?id=1160 分析 § 1 中位数 首先我们考虑,若有x1 & ...
- 【图论-最短路】【P3393】逃离僵尸岛
传送门 Description 小a住的国家被僵尸侵略了!小a打算逃离到该国唯一的国际空港逃出这个国家. 该国有N个城市,城市之间有道路相连.一共有M条双向道路.保证没有自环和重边. K个城市已经被僵 ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造
A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...
- 使用VS2010编译MongoDB C++驱动详解
最近为了解决IM消息记录的高速度写入.多文档类型支持的需求,决定使用MongoDB来解决. 考虑到MongoDB对VS版本要求较高,与我现有的VS版本不兼容,在leveldb.ssdb.redis.h ...
- linux环境下,接着lnmp,安装redis
linux环境下,安装redis 操作记录: 回到家目录 cd ~查看 ls进入 lump cd lnmp1.3-fullls??? sudo ./addons.sh //---进入后选 ...
- Java中的String为什么是不可变的? — String源码分析
原文地址:http://www.importnew.com/16817.html 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为 ...
- Log4J使用实例---日志进行邮件发送或是存入数据库
部分转摘:http://blog.csdn.net/azhao_dn/article/details/9118667 1.根类别(在类别层次结构的顶部,即全局性的日志级别) 配置根Logger,其语法 ...
- MongoDB入门(1)- MongoDB简介
什么是MongoDB NoSQL NoSQL systems are also sometimes called "Not only SQL" to emphasize that ...
- 编辑器vi命令
代码: # vi + 文件名 //将光标放在文档最下面 进入编辑器后: i:插入 x:删除 w:保存 q:退出不保存 q!:强制退出不保存 wq:保存并退出