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:
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
这道题初学者或多或少都参考了网上的答案,主要难点有以下一、要理解正则表达式中.和*的含义,“.”代表任意字符,但是“a*”代表“”,“a”,“aa”,“aaa”……这一点比较容易让人犯迷糊二、必须完全比配,即isMatch("aa","aaa") → false三、第一个参数String s 是不含有“.”和“*”,因此不要把程序复杂化Java实现如下:
static public boolean isMatch(String s, String p) {
        // 如果从s长度入手,s.length() == 0时("","a*")、("","a*b*")都会返回true
            if(p.length() == 0)
                return s.length() == 0;
            if(p.length() == 1)
                return s.length() == 1 && (p.charAt(0) == ‘.‘ || s.charAt(0) == p.charAt(0));
            if(p.charAt(1) != ‘*‘){
                if(s.length() == 0 || (p.charAt(0) != ‘.‘ && s.charAt(0) != p.charAt(0)))
                    return false;
                return isMatch(s.substring(1), p.substring(1));    

            }else {
                if(isMatch(s, p.substring(2)))
                    return true;
                else{
                    int len = s.length();//空间换时间
                    int i = 0;
                    while(i<len && (p.charAt(0) == ‘.‘ || p.charAt(0) == s.charAt(i))){
                        if(isMatch(s.substring(i+1), p.substring(2)))
                            return true;
                        i++;
                    }
                }
                    return false;
                }
        }
				
时间: 2024-08-23 22:57:31

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

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

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

[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