[string]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 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

1.递归-->超时

/*
p的任何一个位置,字符有三种可能
1.?:只能匹配当个字符
2.*:可以匹配任意字符
3.处理?和*的任意字符
可以使用递归:
s[i]和p[j]匹配,则继续匹配下一位置
s[i]和p[j]不匹配,返回假
特殊情况:
1.s到了末尾,p未到末尾,需要检查p剩余的字符是否含有除*外的其他字符
2.s未到末尾,p到了末尾,返回假
3.s到了末尾,p到了末尾,返回真
*/
class Solution {
public:
    bool isMatch(string s,int startS,string p,int startP)
    {
        int sSize = s.size();
        int pSize = p.size();
        if(startS < sSize  && startP < pSize){
            if(s[startS]==p[startP] || p[startP]==‘?‘){
                return isMatch(s,startS+1,p,startP+1);
            }
            if(p[startP]==‘*‘){
                return isMatch(s,startS+1,p,startP) || isMatch(s,startS,p,startP+1) || isMatch(s,startS+1,p,startP+1);
            }
            return false;
        }else if(startS==sSize && startP<pSize){
            for(int i=startP;i<pSize;i++){
                if(p[i]!=‘*‘){
                    return false;
                }
            }
            return true;
        }else if(startS<sSize && startP==pSize){
            return false;
        }else{
            return true;
        }
    }
    bool isMatch(string s, string p) {
        return isMatch(s,0,p,0);
    }
};
时间: 2024-11-08 17:30:41

[string]Wildcard Matching的相关文章

【leetcode】Wildcard Matching

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 partia

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

[LintCode] Wildcard Matching

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

[LeetCode][JavaScript]Wildcard Matching

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 partia

LeetCode 044 Wildcard Matching

题目要求: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 p

[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 shoul

【LeetCode】Wildcard Matching 串匹配 动态规划

题目:Wildcard Matching <span style="font-size:18px;">/*LeetCode WildCard matching * 题目:给定一个目标串和一个匹配串,判定是否能够匹配 * 匹配串中的定义:字符---->字符,*---->0个.1个或者多个字符,?------>对应任意一个字符 * 思路:动态规划:*:dp[i][j] = dp[i][j-1] || dp[i-1][j] * ? || s[i] == p[i]

Regular Expression Matching &amp; Wildcard Matching

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

044 Wildcard Matching

044 Wildcard Matching 这道题直观想法是用dp 复杂度 O(mn) 代码如下, 但是在用python的时候会超时 class Solution: # @param {string} s # @param {string} p # @return {boolean} def isMatch(self, s, p): m, n = len(p), len(s) dp = [False for i in range(0, n+1)] dp[0] = True for j in ra