LeetCode(10) - Regular Expression Matching

  非常challenge的一道题,考虑的情况特别多,当考虑到了之后,还要考虑怎么处理。题目要求是,给你两个字符串,判断正则字符串能不能表示待检字符串,一个是"aabb",另外一个是正则表达式"a*.*"。该正则表达式只有两种特殊符号,一种是‘.‘,代表的是任意字符,另一种是‘*‘,它单独出现没有任何意义,要搭配前一个如"a*",则说明a可以连续出现零到多次,根据这个定义,上面给的例子返回的就是true。总体来说,它要注意的是以下几种情况(s是待检字符串,p是正则字符串):

  1. s和p的长度不一定相等,甚至当s遍历完的时候,p还遍历处理完,如"a" -> "ab*c*"(返回的也是true),但p遍历完时s一定得遍历完。
  2. 当p.charAt(indexP + 1)不为‘*‘的时候,p和s得一一对应或者p是‘.‘(为什么要不为‘*‘才一一对应?详细看第四点)。
  3. 注意到*后还要接同一个字母的情况,例如"aaa" -> "a*a"(返回的也是true,处理的时候不能单纯因为碰到*就直接把s走到底)。
  4. p开头的字符有可能是没有用,例如"aaa" -> "b*a*"的情况(返回也是true)。

  代码如下:

  

 1 public class Solution {
 2     public boolean isMatch(String s, String p) {
 3         return isMatch(s,0,p,0);
 4     }
 5
 6     private boolean isMatch(String s, int indexS, String p, int indexP) {
 7         //以p的结束作为递归的结束。考虑到"a" -> "ab*c*"这种情况。
 8         if (indexP == p.length()) return indexS == s.length();
 9
10         //当p+1不为‘*‘的时候,则需要一一对应。
11         if (indexP == p.length() - 1 || (indexP < p.length() - 1 && p.charAt(indexP + 1) != ‘*‘)) {
12             if (indexS < s.length() && (p.charAt(indexP) == ‘.‘ || p.charAt(indexP) == s.charAt(indexS))) {
13                 return isMatch(s,indexS+1,p,indexP+1);
14             }
15             else return false;
16         }
17         //当p+1为‘*‘的时候,s如果和p相等,因为有*,所以s = s + 1,直到s != p为止,同时还要注意"aaa" -> "a*a"这种情况。
18         else {
19             while (indexS < s.length() && (p.charAt(indexP) == s.charAt(indexS) || p.charAt(indexP) == ‘.‘)) {
20                 //防止"aaa" -> "a*a"这种情况。
21                 if (isMatch(s,indexS,p,indexP+2)) return true;
22                 indexS++;
23             }
24             //如果像"aaa" -> "b*a*"这种情况,是不会走上面循环的,所以只需要改变p,不需要改变s。
25             return isMatch(s,indexS,p,indexP+2);
26         }
27     }
28 }
时间: 2024-12-22 13:05:30

LeetCode(10) - Regular Expression Matching的相关文章

LeetCode 10: 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

[LEETCODE][PYTHON][DP]REGULAR EXPRESSION MATCHING

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.'*' Matches zero or more of the preceding element. The matchin

LeetCode(10)Regular Expression Matching

题目如下: Python代码: # -*- coding:utf-8 -*- def ismatch(s,p): #先将dp[s+1][p+1]二维数组全置为False dp = [[False] * (len(s) + 1) for _ in range(len(p)+1)] dp[0][0] = True for i in range(1,len(p)): dp[i+1][0] = dp[i-1][0] and p[i] == '*' for i in range(len(p)): for

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

LeetCode (10): Regular Expression Matching [HARD]

https://leetcode.com/problems/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 ent

(待解决)LeetCode 10. Regular Expression Matching 解题报告

10. Regular Expression Matching 提交网址: https://leetcode.com/problems/regular-expression-matching/ Total Accepted: 79548 Total Submissions: 361279 Difficulty: Hard Implement regular expression matching with support for '.' and '*'. '.' Matches any sing

Leetcode 10 regular expression matching (正则表达式匹配) (动态规划)

Leetcode 10 问题描述 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 entir

Java [leetcode 10] Regular Expression Matching

问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string

【leetcode】10.Regular Expression Matching

题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. 解题思路: 这道题如果只考虑“.”的话其实很好完成,所以解题的关键在于处理“*”的情况.以为“*”与前一个字母有关,所以应该整体考虑ch*……的情况.ch*可以匹配0-n个s的字符串