【leetcode】44. Wildcard Matching

题目如下:

解题思路:本题和【leetcode】97. Interleaving String非常相似,同样可以采用动态规划的方法。记dp[i][j] = 1或者0 表示pattern[0:i]是否匹配string[0:j] ,如果pattern[i] == string[j] 或者 pattern[i] == ‘?‘,那么dp[i][j]  = dp[i-1][j-1];如果pattern[i] = ‘*‘ 则复杂一些,因为‘*‘可以匹配s[j-1],s[j] 或者不匹配,因此只要满足其中任意一种情况都视为匹配,得出递推关系式:dp[i][j] = max(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])。

代码如下:

class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        np = ‘‘

        #合并连续出现的*,提高效率
        for i in p:
            if len(np) == 0:
                np += i
            elif np[-1] == i and i == ‘*‘:
                continue
            else:
                np += i

        #pattern和string都加上‘#‘开头,处理pattern以*开头,但是又不做匹配的情况
        np  = ‘#‘ + np
        p = np
        s = ‘#‘ + s

        dp = [[0] * len(s) for i in p]
        #print dp

        dp[0][0] = 1

        for i in range(len(dp)):
            for j in range(len(dp[i])):
                if p[i] == ‘*‘:
                    if i > 0 and j > 0:
                        dp[i][j] = max(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])
                    elif i > 0 and j == 0:
                        dp[i][j] = dp[i-1][j]
                    elif i == 0 and j > 0:
                        dp[i][j] = dp[i][j-1]
                elif i > 0 and j > 0 and (p[i] == ‘?‘ or  p[i] == s[j]):
                    dp[i][j] = dp[i-1][j-1]
        #print dp
        return dp[-1][-1] == 1

原文地址:https://www.cnblogs.com/seyjs/p/9559148.html

时间: 2024-10-09 12:17:27

【leetcode】44. Wildcard Matching的相关文章

【一天一道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

【leetcode】Regular Expression Matching (hard) ★

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

【剑指offer】19、正则表达式匹配 && 【Leetcode】44、Wildcard Matching

题目一 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式.例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配 思路 用动态规划来解决 规定 若,则str[0~i-1]和pattern[0~j-1]匹配 1.当p[j

【leetcode】Regular Expression 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

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"