10. Regular Expression Matching && 44. Wildcard Matching

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:
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
public class Solution {
    public boolean isMatch(String s, String p) {
        return isMatch(s, 0, p, 0);
    }

    private boolean isMatch(String s, int si, String p, int pi) {
        if(pi == p.length())
            return si == s.length(); 

        char pCurrent = p.charAt(pi);
        if(pi+1<p.length() && p.charAt(pi+1) == ‘*‘) //if followed by star
        {
            if(isMatch(s, si, p, pi+2)) //skip the first star part.
                return true;
            if(si<s.length() && (s.charAt(si) == pCurrent || pCurrent == ‘.‘) && isMatch(s, si+1, p, pi))
                return true;
            return false;
        }
        else //match single character
        {
            if(si == s.length())
                return false; //if s is already at the end.
            if(pCurrent != s.charAt(si) && pCurrent != ‘.‘)
                return false; //cannot find the match in s.
            return isMatch(s, si+1, p, pi+1);
        }
    }
}
时间: 2024-10-14 10:06:11

10. Regular Expression Matching && 44. Wildcard Matching的相关文章

(待解决)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

10. Regular Expression Matching(hard)

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

刷题10. Regular Expression Matching

一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处理有问题,始终无法成功. #include<iostream> using namespace std; class Solution{ public: bool isMatch(string s,string p){ bool result = true; if(s.length()<=0

【一天一道LeetCode】#44. Wildcard Matching

一天一道LeetCode系列 (一)题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not par

蠡口44. Wildcard Matching

两个字符串,比较后返回T or F,题目很像10 Regular Expression Matching.一样使用动态规划只是在状态转移的时候有点tricky. 1.建立dp矩阵:dp[i][j]: if s[0:i] matches p[0:j], 注意字符串s[0:i]不包括s[i],所以dp矩阵的size应该为(lens+1)*(lenp+1),初始值全为F: 2.初始化边界条件:dp[0][0]表示""(空字符)与""(空字符)是否match,当然是True

44. Wildcard Matching(js)

44. Wildcard Matching Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching

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

题目描述: 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的字符串

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