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) Some examples:
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

解法1:

  这道题中的*表示*之前的那个字符可以有0个,1个或是多个,就是说,字符串a*b,可以表示b或是aaab,即a的个数任意;字符串.*b,可以表示b或是xyzb。需要用递归Recursion来解,大概思路如下:(原字符串为s,正则式为p)

  - 若p为空:

    • 若s也为空,返回true,反之返回false

  - 若p的长度为1:

    • 若s长度也为1,且相同或是p为'.'则返回true,反之返回false

  - 若p的第二个字符不为*:

    • 若此时s为空返回false,否则判断首字符是否匹配,且从各自的第二个字符开始调用递归函数匹配

  - 若p的第二个字符为*:

    • 若s不为空且字符匹配,调用递归函数匹配s和去掉前两个字符的p,若匹配返回true,否则s去掉首字母
    • 否则,返回调用递归函数匹配s和去掉前两个字符的p的结果
public class Solution {
public boolean isMatch(String s, String p) {
if (p.isEmpty()) {
if (s.isEmpty()) return true;
return false;
} if (p.length() == 1)
return (s.length() == 1 && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')); if (p.charAt(1) != '*')
return (s.length() > 0 && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.') && isMatch(s.substring(1), p.substring(1))); while (!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')) {
if (isMatch(s, p.substring(2))) return true;
s = s.substring(1);
}
return isMatch(s, p.substring(2));
}
}

  上面的方法可以写的更加简洁一些,但是整个思路还是一样的,我们先来判断p是否为空,若为空则根据s的为空的情况返回结果。当p的第二个字符为*号时,由于*号前面的字符的个数可以任意,可以为0,那么我们先用递归来调用为0的情况,就是直接把这两个字符去掉再比较,或者当s不为空,且第一个字符和p的第一个字符相同时,我们再对去掉首字符的s和p调用递归,注意p不能去掉首字符,因为*号前面的字符可以有无限个;如果第二个字符不为*号,那么我们就老老实实的比较第一个字符,然后对后面的字符串调用递归,参见代码如下:

public class Solution {
public boolean isMatch(String s, String p) {
if (p.isEmpty()) {
if (s.isEmpty()) return true;
return false;
} if (p.length() > 1 && p.charAt(1) == '*')
return (isMatch(s, p.substring(2)) || (!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.') && isMatch(s.substring(1), p))); return (!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.') && isMatch(s.substring(1), p.substring(1)));
}
}

解法2:

  用动态规划DP来解,用二维数组 dp[i][j] 表示s的前i个字符s[0,i) 和p的前j个字符p[0,j)是否匹配,分以下几种情况:

  • 当前字符 p[j-1]=='*' 时,*前的字符可能不出现或者至少出现一次,即:
dp[i][j] = dp[i][j - 2] || (i > 0 && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.') && dp[i - 1][j]);
    • dp[i][j - 2] 表示*前的字符一次都不出现,即与 s[0,i)和p[0,j-2) 的匹配情况相同;
    • i > 0 && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.') && dp[i - 1][j] 表示*前的字符至少出现一次,即与 s[0,i-1)和p[0,j)de匹配情况相同
  • 当前字符 p[j-1]!='*' 时,当前字符 s[i-1] 和 p[j-1]必须匹配,同时之前的字符串 s[0,i-1)和p[0,j-1)必须匹配, s[0,i)和p[0,j)才能匹配
dp[i][j] = i > 0 && (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.') && dp[i - 1][j - 1]

整体代码如下:

public class Solution {
public boolean isMatch(String s, String p) {
boolean[][] dp = new boolean[s.length() + 1][p.length() + 1];
dp[0][0] = true; for (int i = 0; i <= s.length(); i++) {
for (int j = 1; j <= p.length(); j++) { // j从1开始,因为i>0,j=0的情况肯定不匹配
if (j > 1 && p.charAt(j - 1) == '*') {
dp[i][j] = dp[i][j - 2] || (i > 0 && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.') && dp[i - 1][j]); // dp[i][j-2]表示*前的字符匹配0次,后面的表示匹配1次以上
} else {
// 不为*的时候,需要当前两个字符匹配,同时dp[i-1][j-1]
dp[i][j] = i > 0 && (s.charAt(i - 1) == p.charAt(j - 1) || p.charAt(j - 1) == '.') && dp[i - 1][j - 1];
}
}
}
return dp[s.length()][p.length()];
}
}

[LeetCode] 10. Regular Expression Matching ☆☆☆☆☆的更多相关文章

  1. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  2. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  3. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

  4. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  5. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  6. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  7. Java [leetcode 10] Regular Expression Matching

    问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

  8. [leetcode]10. Regular Expression Matching正则表达式的匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  9. 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]

    题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...

随机推荐

  1. Twaver的mono-desiner导出的json文件解析

    以画的交换机为例,其他大概都差不多. 利用Twaver做出交换机模型如图1所示,其中,每一个端口都是一个单独的对象.具体Twaver操作流程参见网址:http://twaver.servasoft.c ...

  2. POJ 3675 Telescope(简单多边形和圆的面积交)

    Description Updog is watching a plane object with a telescope. The field of vision in the telescope ...

  3. Rightmost Digit(最后一位数字)

    Description Given a positive integer N, you should output the most right digit of N^N.    Input The ...

  4. "Hello world!"团队第八次会议

    Scrum会议 今天是我们"Hello world!"团队第八次召开会议,博客内容是: 1.会议时间 2.会议成员 3.会议地点 4.会议内容 5.todo list 6.会议照片 ...

  5. 关于GenericJDBCException的问题

    在spring和hibernate整合的初步阶段,还没有编辑hibernate.cfg.xml这个文件,只有一个beans.xml文件.此时遇到了一个bug. Exception in thread ...

  6. TCP系列30—窗口管理&流控—4、Cork算法

    一.Cork算法概述 Cork算法与Nagle算法类似,也有人把Cork算法称呼为super-Nagle.Nagle算法提出的背景是网络因为大量小包小包而导致利用率低下产生网络拥塞,网络发生拥塞的时候 ...

  7. Expected Conditions的常用函数

    Expected Conditions的使用场景有两种  1.直接在断言中使用  2.与WebDriverWait配合使用,动态等待页面上元素出现或者消失 1. title_is: 判断当前页面的ti ...

  8. Directory类的使用、Alt+Shift+F10可以查看其命名空间

    对于一个对象,按下Alt+Shift+F10可以查看其命名空间. Directory类的使用 using System; using System.Collections.Generic; using ...

  9. Mysql查询优化从入门到跑路(一)数据库与关系代数

    1.怎样才算是数据库?     ACID,是指在数据库管理系统中事务所具有的四个特性     1)原子性     2)一致性     3)隔离性     4)持久性       关系数据库,基于关系代 ...

  10. [计算机网络] DNS何时使用TCP协议,何时使用UDP协议

    DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP栈也算是个另类.但很少有人知道DNS分别在什么情况下使用这两种协议. 先简单介绍下TCP与UDP. ...