[LeetCode]wildcard matching通配符实现TLE

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 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", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false

直接的思路很简单,两个字符串从头开始匹配,不匹配直接返回false,匹配,则两个指针都加1,匹配子字符串,所以自然而然就想用递归来实现。复杂一点的是*号,匹配任意长度的字符串,所以*的判断也可以循环的递归isMatch判断*后的子字符串。实现如下:
class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        if (‘\0‘ == *s && ‘\0‘ == *p)
        {
            return true;
        }
        if (‘*‘ == *s)
        {
            return asteriskMatch(s, p);
        }
        if (‘*‘ == *p)
        {
            return asteriskMatch(p, s);
        }
        if (*s == *p || ‘?‘ == *s || ‘?‘ == *p)
        {
            s++;
            p++;
            return isMatch(s, p);
        }
        else
        {
            return false;
        }
    }
private:
    bool asteriskMatch(const char *asterisk, const char *p)
    {
        asterisk++;
        if (‘*‘ == *asterisk)
        {
            return asteriskMatch(asterisk, p);
        }
        while (*p != ‘\0‘)
        {
            if (isMatch(asterisk, p))
            {
                return true;
            }
            p++;
        }
        if (‘\0‘ == *asterisk && ‘\0‘ == *p)
        {
            return true;
        }
        return false;
    }
};

这样实现代码比较清爽,但是*的判断效率明显不高,提交,果然Time Limit Exceeded.没通过的用例是:

Last executed input: "abbabaaabbabbaababbabbbbbabbbabbbabaaaaababababbbabababaabbababaabbbbbbaaaabababbbaabbbbaabbbbababababbaabbaababaabbbababababbbbaaabbbbbabaaaabbababbbbaababaabbababbbbbababbbabaaaaaaaabbbbbaabaaababaaaabb", "**aa*****ba*a*bb**aa*ab****a*aaaaaa***a*aaaa**bbabb*b*b**aaaaaaaaa*a********ba*bbb***a*ba*bb*bb**a*b*bb"

应该改用动态规划或者贪心法试试,不过总是觉得用递归实现比较优雅,而且看Discuss里很多人用DP也还是TLE,不行了,太困了,明天再改吧

[LeetCode]wildcard matching通配符实现TLE,布布扣,bubuko.com

时间: 2024-08-25 06:53:47

[LeetCode]wildcard matching通配符实现TLE的相关文章

[LeetCode]wildcard matching通配符实现之动态规划

前天wildcard matching没AC,今天接着搞,改用动态规划.题目再放一次: '?'匹配任意字符,'*'匹配任意长度字符串 Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*&q

[LeetCode]wildcard matching通配符实现之贪心法

前天用递归LTE,昨天用动态规划LTE,今天接着搞,改用贪心法.题目再放一次: '?'匹配任意字符,'*'匹配任意长度字符串 Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "*"

[LeetCode] Wildcard Matching 通配符匹配

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 partial). The function p

[LeetCode]Wildcard Matching 通配符匹配(贪心)

一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,const char *p){ if(flag)return; if(id0>=n){ if(id1>=m)flag=1; else{ int j=0; while(j<m&&p[j]=='*')j++; if(j>=m)flag=1; } return; } else i

LeetCode: Wildcard Matching [043]

[题目] 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 partial). The funct

Leetcode: Wildcard Matching. java

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 partial). The function p

leetCode 44.Wildcard Matching (通配符匹配) 解题思路和方法

Wildcard Matching '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s,

LeetCode: Wildcard Matching 解题报告

Wildcard MatchingImplement 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 partial)

(Java) LeetCode 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 should cover the enti