{"id":499,"date":"2020-09-26T10:49:00","date_gmt":"2020-09-26T02:49:00","guid":{"rendered":"http:\/\/39.96.58.60\/?p=499"},"modified":"2022-10-18T16:39:03","modified_gmt":"2022-10-18T08:39:03","slug":"leetcode-925-%e9%95%bf%e6%8c%89%e9%94%ae%e5%85%a5","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=499","title":{"rendered":"Leetcode 925. \u957f\u6309\u952e\u5165"},"content":{"rendered":"<p>\u4f60\u7684\u670b\u53cb\u6b63\u5728\u4f7f\u7528\u952e\u76d8\u8f93\u5165\u4ed6\u7684\u540d\u5b57&nbsp;<code>name<\/code>\u3002\u5076\u5c14\uff0c\u5728\u952e\u5165\u5b57\u7b26&nbsp;<code>c<\/code>&nbsp;\u65f6\uff0c\u6309\u952e\u53ef\u80fd\u4f1a\u88ab<em>\u957f\u6309<\/em>\uff0c\u800c\u5b57\u7b26\u53ef\u80fd\u88ab\u8f93\u5165 1 \u6b21\u6216\u591a\u6b21\u3002<\/p>\n<p>\u4f60\u5c06\u4f1a\u68c0\u67e5\u952e\u76d8\u8f93\u5165\u7684\u5b57\u7b26&nbsp;<code>typed<\/code>\u3002\u5982\u679c\u5b83\u5bf9\u5e94\u7684\u53ef\u80fd\u662f\u4f60\u7684\u670b\u53cb\u7684\u540d\u5b57\uff08\u5176\u4e2d\u4e00\u4e9b\u5b57\u7b26\u53ef\u80fd\u88ab\u957f\u6309\uff09\uff0c\u90a3\u4e48\u5c31\u8fd4\u56de&nbsp;<code>True<\/code>\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>name = \"alex\", typed = \"aaleex\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>true\n\n<strong>\u89e3\u91ca\uff1a<\/strong>'alex' \u4e2d\u7684 'a' \u548c 'e' \u88ab\u957f\u6309\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>name = \"saeed\", typed = \"ssaaedd\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>false\n\n<strong>\u89e3\u91ca\uff1a<\/strong>'e' \u4e00\u5b9a\u9700\u8981\u88ab\u952e\u5165\u4e24\u6b21\uff0c\u4f46\u5728 typed \u7684\u8f93\u51fa\u4e2d\u4e0d\u662f\u8fd9\u6837\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>name = \"leelee\", typed = \"lleeelee\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>true\n\n<\/pre>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>name = \"laiden\", typed = \"laiden\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>true\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u957f\u6309\u540d\u5b57\u4e2d\u7684\u5b57\u7b26\u5e76\u4e0d\u662f\u5fc5\u8981\u7684\u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li><code>name.length &lt;= 1000<\/code><\/li>\n<li><code>typed.length &lt;= 1000<\/code><\/li>\n<li><code>name<\/code> \u548c&nbsp;<code>typed<\/code>&nbsp;\u7684\u5b57\u7b26\u90fd\u662f\u5c0f\u5199\u5b57\u6bcd\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u53cc\u6307\u9488\u3001 \u5b57\u7b26\u4e32\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\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8681.82% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8618.18% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def isLongPressedName(self, name: str, typed: str) -&gt; bool:\n        p, q = 0, 0     #\u6307\u9488\n        while p &lt; len(name) and q &lt; len(typed):\n            if name[p] == typed[q]: # \u5982\u679c\u5bf9\u5e94\u4f4d\u7f6e\u5339\u914d\uff0c\u6307\u9488\u540c\u65f6\u540e\u79fb\uff0c\u5339\u914d\u4e0b\u4e00\u4e2a\n                p += 1\n                q += 1\n            else:\n                if q &gt; 0 and typed[q-1] == typed[q]:    # \u5982\u679ctyped\u5b58\u5728\u8fde\u51fb\uff0c\u5339\u914d\u4e0b\u4e00\u4e2a\n                    q += 1\n                else:   # \u4e0d\u5b58\u5728\u8fde\u51fb\uff0c\u8fd4\u56deFalse\n                    return False\n        if p &gt;= len(name):   # name\u5339\u914d\u5b8c\n            if q &gt;= len(typed):  # typed \u4e5f\u5339\u914d\u5b8c\n                return True\n            else:\n                while q &lt; len(typed):   # \u82e5typed\u6ca1\u6709\u5339\u914d\u5b8c\uff0c\u4e14\u540e\u7eed\u8fde\u51fb\uff0c\u8fd4\u56deTrue\n                    if typed[q] != typed[q-1]:  # \u82e5\u4e0d\u5b58\u5728\u8fde\u51fb\uff0c\u5219\u8fd4\u56deFalse\n                        return False\n                    q += 1\n                return True\n        else:\n            return False<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4f60\u7684\u670b\u53cb\u6b63\u5728\u4f7f\u7528\u952e\u76d8\u8f93\u5165\u4ed6\u7684\u540d\u5b57&nbsp;name\u3002\u5076\u5c14\uff0c\u5728\u952e\u5165\u5b57\u7b26&nbsp;c&nbsp;\u65f6\uff0c\u6309\u952e\u53ef\u80fd\u4f1a&#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":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/499"}],"collection":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=499"}],"version-history":[{"count":1,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/499\/revisions"}],"predecessor-version":[{"id":500,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/499\/revisions\/500"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}