LeetCode 010 Regular Expression Matching - Java

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

定位:困难题

题目给出两种匹配规则,即‘.‘能匹配任何字符,‘*‘可以将前面一个字符重复0到任意次。

此时我们将‘*‘与其前一个字符合看为一个单元考虑。存在以下情况:

  • 两个字符串长度均为0,返回true;
  • 前一个长度为0,后一个均为含‘*‘单元,返回true;
  • 取前一个的前项字符与后一个前项字符,如若前一个前项为重复字符,取到不重复位置,后一个判断是否有‘*‘单元进行匹配,匹配正确去除已匹配部分将其余递归,不匹配返回false。

Java实现:

 1 public class Solution {
 2     public boolean isMatch(String s, String p) {
 3         if(p.length()==0){
 4             return s.length()==0;
 5         }
 6         else if(s.length()==0) {
 7             if(p.length()>1&&p.charAt(1)==‘*‘) return isMatch(s, p.substring(2));
 8             else return false;
 9         } else if(p.length()>1 && p.charAt(1)==‘*‘) {
10             if(isMatch(s,p.substring(2))) return true;
11             else if(s.charAt(0)==p.charAt(0)||p.charAt(0)==‘.‘) {
12                 return isMatch(s.substring(1), p);
13             } else return false;
14         } else {
15             return (s.charAt(0)==p.charAt(0) || p.charAt(0)==‘.‘) && isMatch(s.substring(1), p.substring(1));
16         }
17     }
18 }
时间: 2024-10-09 02:43:02

LeetCode 010 Regular Expression Matching - Java的相关文章

LeetCode 010 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 shou

LeetCode——010 Regular Expression Matching

title: LeetCode--010 Regular Expression Matching author: zzw top: false toc: true mathjax: false email: [email protected] date: 2020-02-19 22:05:32 updated: 2020-02-19 22:05:32 img: summary: categories: LeetCode tags: 字符匹配 --- Description Given an in

Java for LeetCode 010 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] 010. Regular Expression Matching (Hard) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 010.Regular_Expression_Matching (Hard) 链接: 题目:https://oj.leetcode.com/problems/regular-expression-matching/ 代码(github):https://github.com/illuz/leetcode 题意: 给

LeetCode 10 Regular Expression Matching (C,C++,Java,Python)

Problem: 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

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解析 -Java实现

Q: 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

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

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