{"id":2127,"date":"2018-03-16T02:48:11","date_gmt":"2018-03-16T09:48:11","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2127"},"modified":"2018-03-16T02:49:15","modified_gmt":"2018-03-16T09:49:15","slug":"leetcode-392-is-subsequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-392-is-subsequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 392. Is Subsequence"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u95ees\u662f\u4e0d\u662ft\u7684\u5b50\u5e8f\u5217\u3002<\/p>\n<h1>Problem:<\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/is-subsequence\/description\/\">https:\/\/leetcode.com\/problems\/is-subsequence\/description\/<\/a><\/p>\n<p>Given a string\u00a0<b>s<\/b>\u00a0and a string\u00a0<b>t<\/b>, check if\u00a0<b>s<\/b>\u00a0is subsequence of\u00a0<b>t<\/b>.<\/p>\n<p>You may assume that there is only lower case English letters in both\u00a0<b>s<\/b>\u00a0and\u00a0<b>t<\/b>.\u00a0<b>t<\/b>\u00a0is potentially a very long (length ~= 500,000) string, and\u00a0<b>s<\/b>is a short string (&lt;=100).<\/p>\n<p>A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,\u00a0<code>\"ace\"<\/code>\u00a0is a subsequence of\u00a0<code>\"abcde\"<\/code>\u00a0while\u00a0<code>\"aec\"<\/code>\u00a0is not).<\/p>\n<p><b>Example 1:<\/b><br \/>\n<b>s<\/b>\u00a0=\u00a0<code>\"abc\"<\/code>,\u00a0<b>t<\/b>\u00a0=\u00a0<code>\"ahbgdc\"<\/code><\/p>\n<p>Return\u00a0<code>true<\/code>.<\/p>\n<p><b>Example 2:<\/b><br \/>\n<b>s<\/b>\u00a0=\u00a0<code>\"axc\"<\/code>,\u00a0<b>t<\/b>\u00a0=\u00a0<code>\"ahbgdc\"<\/code><\/p>\n<p>Return\u00a0<code>false<\/code>.<\/p>\n<p><b>Follow up:<\/b><br \/>\nIf there are lots of incoming S, say S1, S2, &#8230; , Sk where k &gt;= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?<\/p>\n<h1>Solution: Brute force<\/h1>\n<p>Time Complexity: O(|s| + |t|)<\/p>\n<p>Space Complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 65 ms (beats 99.37%)\r\nclass Solution {\r\npublic:\r\n  bool isSubsequence(const string&amp; s, const string&amp; t) {\r\n    int i = 0;\r\n    const int n = t.length();\r\n    for (const char c : s) {\r\n      while (i &lt; n &amp;&amp; t[i] != c) ++i;      \r\n      if (i++ == n) return false;\r\n    }\r\n    return true;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u95ees\u662f\u4e0d\u662ft\u7684\u5b50\u5e8f\u5217\u3002 Problem: https:\/\/leetcode.com\/problems\/is-subsequence\/description\/ Given a string\u00a0s\u00a0and a string\u00a0t, check if\u00a0s\u00a0is subsequence of\u00a0t. You may assume that there is only lower case English letters in both\u00a0s\u00a0and\u00a0t.\u00a0t\u00a0is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[177,4,229],"class_list":["post-2127","post","type-post","status-publish","format-standard","hentry","category-string","tag-medium","tag-string","tag-subsequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2127","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=2127"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2127\/revisions"}],"predecessor-version":[{"id":2130,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2127\/revisions\/2130"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}