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

参考网址:http://blog.csdn.net/pickless/article/details/9787227

代码如下:

class Solution {
public:
    bool initCheck(const char *s, const char *p) {
        int l1 = 0;
        int idx = 0, l2 = 0;

        while (s[l1] != ‘\0‘) {
            l1++;
        }
        while (p[idx] != ‘\0‘) {
            l2 += p[idx++] != ‘*‘ ? 1 : 0;
        }

        return l1 >= l2;
    }

    bool isMatch(const char *s, const char *p) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (!initCheck(s, p)) {
            return false;
        } 

        int l = 0;
        while (p[l++] != ‘\0‘);

        bool prev[l], f[l];
        memset(f, false, sizeof(bool) * l);
        memset(prev, false, sizeof(bool) * l);

        bool isFirst = true;
        for (int i = 0; i < l; i++) {
            if (p[i] == ‘*‘) {
                f[i] = i == 0 || f[i - 1];
            }
            else if ((p[i] == ‘?‘ || p[i] == *s) && isFirst) {
                isFirst = false;
                f[i] = true;
            }
            else {
                break;
            }
        }
        s++;

        while (*(s - 1) != ‘\0‘) {
            memcpy(prev, f, l);
            memset(f, false, sizeof(bool) * l);

            for (int i = 0; i < l; i++) {
                if (prev[i]) {
                    f[i] = f[i] || p[i] == ‘*‘;
                    f[i + 1] = f[i + 1] || p[i + 1] == ‘?‘ || p[i + 1] == *s;
                }
                else if (i == 0 || f[i - 1]) {
                    f[i] = f[i] || p[i] == ‘*‘;
                }
            }
            s++;
        }    

        return f[l - 1];
    }
};
时间: 2024-12-28 18:09:53

LeetCode 044 Wildcard Matching的相关文章

Java for LeetCode 044 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

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

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

【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 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(hard) ★ 大神太牛了

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

(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

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