{"id":9332,"date":"2021-12-31T14:11:02","date_gmt":"2021-12-31T22:11:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9332"},"modified":"2021-12-31T14:11:43","modified_gmt":"2021-12-31T22:11:43","slug":"leetcode-1961-check-if-string-is-a-prefix-of-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1961-check-if-string-is-a-prefix-of-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1961. Check If String Is a Prefix of Array"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;and an array of strings&nbsp;<code>words<\/code>, determine whether&nbsp;<code>s<\/code>&nbsp;is a&nbsp;<strong>prefix string<\/strong>&nbsp;of&nbsp;<code>words<\/code>.<\/p>\n\n\n\n<p>A string&nbsp;<code>s<\/code>&nbsp;is a&nbsp;<strong>prefix string<\/strong>&nbsp;of&nbsp;<code>words<\/code>&nbsp;if&nbsp;<code>s<\/code>&nbsp;can be made by concatenating the first&nbsp;<code>k<\/code>&nbsp;strings in&nbsp;<code>words<\/code>&nbsp;for some&nbsp;<strong>positive<\/strong>&nbsp;<code>k<\/code>&nbsp;no larger than&nbsp;<code>words.length<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code><em>&nbsp;if&nbsp;<\/em><code>s<\/code><em>&nbsp;is a&nbsp;<strong>prefix string<\/strong>&nbsp;of&nbsp;<\/em><code>words<\/code><em>, or&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"iloveleetcode\", words = [\"i\",\"love\",\"leetcode\",\"apples\"]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>\ns can be made by concatenating \"i\", \"love\", and \"leetcode\" together.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"iloveleetcode\", words = [\"apples\",\"i\",\"love\",\"leetcode\"]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong>\nIt is impossible to make s using a prefix of arr.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= words.length &lt;= 100<\/code><\/li><li><code>1 &lt;= words[i].length &lt;= 20<\/code><\/li><li><code>1 &lt;= s.length &lt;= 1000<\/code><\/li><li><code>words[i]<\/code>&nbsp;and&nbsp;<code>s<\/code>&nbsp;consist of only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Concat and test<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isPrefixString(string s, vector<string>& words) {\n    string t;\n    for (const string& w : words) {\n      t += w;\n      if (t.length() >= s.length()) break;\n    }\n    return t == s;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;and an array of strings&nbsp;words, determine whether&nbsp;s&nbsp;is a&nbsp;prefix string&nbsp;of&nbsp;words. A string&nbsp;s&nbsp;is a&nbsp;prefix string&nbsp;of&nbsp;words&nbsp;if&nbsp;s&nbsp;can be made by concatenating the first&nbsp;k&nbsp;strings in&nbsp;words&nbsp;for some&nbsp;positive&nbsp;k&nbsp;no larger than&nbsp;words.length.&#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":[754,222,4],"class_list":["post-9332","post","type-post","status-publish","format-standard","hentry","category-string","tag-concat","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9332","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=9332"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9332\/revisions"}],"predecessor-version":[{"id":9334,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9332\/revisions\/9334"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}