{"id":739,"date":"2020-09-26T11:16:40","date_gmt":"2020-09-26T03:16:40","guid":{"rendered":"http:\/\/39.96.58.60\/?p=739"},"modified":"2022-10-18T16:37:28","modified_gmt":"2022-10-18T08:37:28","slug":"leetcode-44-%e9%80%9a%e9%85%8d%e7%ac%a6%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=739","title":{"rendered":"Leetcode 44. \u901a\u914d\u7b26\u5339\u914d"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32&nbsp;(<code>s<\/code>) \u548c\u4e00\u4e2a\u5b57\u7b26\u6a21\u5f0f&nbsp;(<code>p<\/code>) \uff0c\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301&nbsp;<code>'?'<\/code>&nbsp;\u548c&nbsp;<code>'*'<\/code>&nbsp;\u7684\u901a\u914d\u7b26\u5339\u914d\u3002<\/p>\n<pre>'?' \u53ef\u4ee5\u5339\u914d\u4efb\u4f55\u5355\u4e2a\u5b57\u7b26\u3002\n\n'*' \u53ef\u4ee5\u5339\u914d\u4efb\u610f\u5b57\u7b26\u4e32\uff08\u5305\u62ec\u7a7a\u5b57\u7b26\u4e32\uff09\u3002\n\n<\/pre>\n<p>\u4e24\u4e2a\u5b57\u7b26\u4e32<strong>\u5b8c\u5168\u5339\u914d<\/strong>\u624d\u7b97\u5339\u914d\u6210\u529f\u3002<\/p>\n<p><strong>\u8bf4\u660e:<\/strong><\/p>\n<ul>\n<li><code>s<\/code>&nbsp;\u53ef\u80fd\u4e3a\u7a7a\uff0c\u4e14\u53ea\u5305\u542b\u4ece&nbsp;<code>a-z<\/code>&nbsp;\u7684\u5c0f\u5199\u5b57\u6bcd\u3002<\/li>\n<li><code>p<\/code>&nbsp;\u53ef\u80fd\u4e3a\u7a7a\uff0c\u4e14\u53ea\u5305\u542b\u4ece&nbsp;<code>a-z<\/code>&nbsp;\u7684\u5c0f\u5199\u5b57\u6bcd\uff0c\u4ee5\u53ca\u5b57\u7b26&nbsp;<code>?<\/code>&nbsp;\u548c&nbsp;<code>*<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>\n\ns = \"aa\"\n\np = \"a\"\n\n<strong>\u8f93\u51fa:<\/strong> false\n\n<strong>\u89e3\u91ca:<\/strong> \"a\" \u65e0\u6cd5\u5339\u914d \"aa\" \u6574\u4e2a\u5b57\u7b26\u4e32\u3002<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>\n\ns = \"aa\"\n\np = \"*\"\n\n<strong>\u8f93\u51fa:<\/strong> true\n\n<strong>\u89e3\u91ca:<\/strong>&nbsp;'*' \u53ef\u4ee5\u5339\u914d\u4efb\u610f\u5b57\u7b26\u4e32\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;3:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>\n\ns = \"cb\"\n\np = \"?a\"\n\n<strong>\u8f93\u51fa:<\/strong> false\n\n<strong>\u89e3\u91ca:<\/strong>&nbsp;'?' \u53ef\u4ee5\u5339\u914d 'c', \u4f46\u7b2c\u4e8c\u4e2a 'a' \u65e0\u6cd5\u5339\u914d 'b'\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;4:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>\n\ns = \"adceb\"\n\np = \"*a*b\"\n\n<strong>\u8f93\u51fa:<\/strong> true\n\n<strong>\u89e3\u91ca:<\/strong>&nbsp;\u7b2c\u4e00\u4e2a '*' \u53ef\u4ee5\u5339\u914d\u7a7a\u5b57\u7b26\u4e32, \u7b2c\u4e8c\u4e2a '*' \u53ef\u4ee5\u5339\u914d\u5b57\u7b26\u4e32 \"dce\".\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;5:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong>\n\ns = \"acdcb\"\n\np = \"a*c?b\"\n\n<strong>\u8f93\u51fa:<\/strong> false<\/pre>\n<p>**\u96be\u5ea6**: Hard<\/p>\n<p>**\u6807\u7b7e**: \u8d2a\u5fc3\u7b97\u6cd5\u3001 \u5b57\u7b26\u4e32\u3001 \u52a8\u6001\u89c4\u5212\u3001 \u56de\u6eaf\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a832 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8671.51% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a22.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8621.67% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u52a8\u6001\u89c4\u5212\n\n    \u4f7f\u7528dp[i][j] \u8868\u793a\uff0cp[i]\u4e4b\u524d\u90e8\u5206\u4e0es[j]\u4e4b\u524d\u90e8\u5206\u662f\u5426\u5339\u914d\n    \u4f8b\uff1a\n            ''  a   d   c   e   b\n        ''  T   F   F   F   F   F\n        *   T\n        a   F\n        *   F\n        b   F\n\n        \u8868\u4e2d\uff0c\u7b2c\u4e00\u884c\u7b2c\u4e00\u7c7b\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\n        \u5bf9\u4e8es\u4e2d\u7684\u7a7a\u5b57\u7b26\u4e32\uff0c\u53ea\u6709*\u6216\u8005''\u53ef\u4ee5\u5339\u914d\u3002  \u5f53*\u65f6\uff0cdp[i][0] = dp[i-1][0]\n        p\u4e2d'' \u53ea\u53ef\u4ee5\u5339\u914d''\u3002\u5219 dp[0][j] = False     j!=0\n\n\n            ''  a   d   c   e   b\n        ''  T   F   F   F   F   F\n        *   T   T   T   T   T   T\n        a   F   T   F   F   F   F\n        *   F   T   T   T   T   T\n        b   F   F   F   F   F   T\n\n        \u5bf9\u4e8e\u8868\u4e2d\u5176\u4ed6\u4f4d\u7f6e\uff0c\u5b58\u5728\u4e09\u79cd\u60c5\u51b5:\n            1. p[i] == '*', \u53ef\u4ee5\u5339\u914d\u4e00\u5207\uff0c\u5305\u542b\u7a7a\u5b57\u7b26\u4e32\uff0c\n                \u5219\u5339\u914d\u7a7a\u5b57\u7b26\u4e32\u65f6\uff1adp[i][j] == dp[i-1][j]\n                \u5339\u914d1\u6216\u591a\u4e2a\u5b57\u7b26\u65f6\uff1a  dp[i][j] == dp[i][j-1]\n            2. p[i] == '?', \u53ef\u5339\u914d\u5355\u4e2a\u5b57\u7b26\n                dp[i][j] == dp[i-1][j-1]\n            3. \u5176\u4ed6\u5b57\u7b26\u65f6\uff0c\n                dp[i][j] == p[i]=s[j] and dp[i-1][j-1]\n\"\"\"\nclass Solution:\n    def isMatch(self, s: str, p: str) -&gt; bool:\n        l_s = len(s)\n        l_p = len(p)\n\n        dp = [[False for _ in range(l_s+1)] for _ in range(l_p+1)]\n\n        dp[0][0] = True\n        for i in range(l_p):\n            if p[i] == '*':\n                dp[i+1][0] = dp[i][0]\n\n\n        for i in range(l_p):\n            for j in range(l_s):\n                if p[i] == '*':\n                    dp[i+1][j+1] = dp[i+1][j] or dp[i][j+1]\n                elif p[i] == '?' and s[j] != '':\n                    dp[i+1][j+1] = dp[i][j]\n                elif p[i] == s[j]:\n                    dp[i+1][j+1] = dp[i][j]\n        return dp[-1][-1]<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32&nbsp;(s) \u548c\u4e00\u4e2a\u5b57\u7b26\u6a21\u5f0f&nbsp;(p) \uff0c\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301&nbsp;&#8216;?&#8217;&#038;nbsp&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/739"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=739"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/739\/revisions"}],"predecessor-version":[{"id":740,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/739\/revisions\/740"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=739"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}