{"id":4203,"date":"2018-10-20T20:51:57","date_gmt":"2018-10-21T03:51:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4203"},"modified":"2018-10-20T20:55:25","modified_gmt":"2018-10-21T03:55:25","slug":"leetcode-925-long-pressed-name","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-925-long-pressed-name\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 925. Long Pressed Name"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Your friend is typing his\u00a0<code>name<\/code>\u00a0into a keyboard.\u00a0 Sometimes, when typing a character\u00a0<code>c<\/code>, the key might get\u00a0<em>long pressed<\/em>, and the character will be typed 1 or more times.<\/p>\n<p>You examine the\u00a0<code>typed<\/code>\u00a0characters of the keyboard.\u00a0 Return\u00a0<code>True<\/code>\u00a0if it is possible that it was your friends name, with some characters (possibly none) being long pressed.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>name = <span id=\"example-input-1-1\">\"alex\"<\/span>, typed = <span id=\"example-input-1-2\">\"aaleex\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">true<\/span>\r\n<strong>Explanation: <\/strong>'a' and 'e' in 'alex' were long pressed.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>name = <span id=\"example-input-2-1\">\"saeed\"<\/span>, typed = <span id=\"example-input-2-2\">\"ssaaedd\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">false<\/span>\r\n<strong>Explanation: <\/strong>'e' must have been pressed twice, but it wasn't in the typed output.\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>name = <span id=\"example-input-3-1\">\"leelee\"<\/span>, typed = <span id=\"example-input-3-2\">\"lleeelee\" <\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">true<\/span>\r\n<\/pre>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>name = <span id=\"example-input-4-1\">\"laiden\"<\/span>, typed = <span id=\"example-input-4-2\">\"laiden\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-4\">true<\/span>\r\n<strong>Explanation: <\/strong>It's not necessary to long press any character.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>name.length &lt;= 1000<\/code><\/li>\n<li><code>typed.length &lt;= 1000<\/code><\/li>\n<li>The characters of\u00a0<code>name<\/code>\u00a0and\u00a0<code>typed<\/code>\u00a0are lowercase letters.<\/li>\n<\/ol>\n<h1><strong>Solution: Two Pointers<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua, 0 ms\r\nclass Solution {\r\npublic:\r\n  bool isLongPressedName(string name, string typed) {\r\n    int i = 0;\r\n    int j = 0;\r\n    while (i &lt; name.size() &amp;&amp; j &lt; typed.size()) {      \r\n      if (name[i] == typed[j]) {\r\n        ++i;\r\n        ++j;\r\n      } else if (j &gt; 0 &amp;&amp; typed[j] == typed[j - 1]) {\r\n        ++j;\r\n      } else {\r\n        return false;\r\n      }\r\n    }\r\n    while (j &lt; typed.size() &amp;&amp; typed[j] == typed[j - 1]) ++j;\r\n    return i == name.size() &amp;&amp; j == typed.size();\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ \/ Iterator<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  bool isLongPressedName(string name, string typed) {\r\n    auto it1 = begin(name);\r\n    auto it2 = begin(typed);\r\n    while (it1 != end(name) &amp;&amp; it2 != end(typed)) {      \r\n      if (*it1 == *it2) {\r\n        ++it1;\r\n        ++it2;\r\n      } else if (it2 != begin(typed) &amp;&amp; *it2 == *prev(it2)) {\r\n        ++it2;\r\n      } else {\r\n        return false;\r\n      }\r\n    }\r\n    while (it2 != end(typed) &amp;&amp; *it2 == *prev(it2)) ++it2;\r\n    return it1 == end(name) &amp;&amp; it2 == end(typed);\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Your friend is typing his\u00a0name\u00a0into a keyboard.\u00a0 Sometimes, when typing a character\u00a0c, the key might get\u00a0long pressed, and the character will be typed 1&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[4,175],"class_list":["post-4203","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-string","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4203"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4203\/revisions"}],"predecessor-version":[{"id":4207,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4203\/revisions\/4207"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}