{"id":9703,"date":"2022-04-30T22:25:24","date_gmt":"2022-05-01T05:25:24","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9703"},"modified":"2022-04-30T22:26:49","modified_gmt":"2022-05-01T05:26:49","slug":"leetcode-2255-count-prefixes-of-a-given-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2255-count-prefixes-of-a-given-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2255. Count Prefixes of a Given String"},"content":{"rendered":"\n<p>You are given a string array&nbsp;<code>words<\/code>&nbsp;and a string&nbsp;<code>s<\/code>, where&nbsp;<code>words[i]<\/code>&nbsp;and&nbsp;<code>s<\/code>&nbsp;comprise only of&nbsp;<strong>lowercase English letters<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number of strings<\/strong>&nbsp;in<\/em>&nbsp;<code>words<\/code>&nbsp;<em>that are a&nbsp;<strong>prefix<\/strong>&nbsp;of<\/em>&nbsp;<code>s<\/code>.<\/p>\n\n\n\n<p>A&nbsp;<strong>prefix<\/strong>&nbsp;of a string is a substring that occurs at the beginning of the string. A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters within a string.<\/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> words = [\"a\",\"b\",\"c\",\"ab\",\"bc\",\"abc\"], s = \"abc\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>\nThe strings in words which are a prefix of s = \"abc\" are:\n\"a\", \"ab\", and \"abc\".\nThus the number of strings in words which are a prefix of s is 3.<\/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> words = [\"a\",\"a\"], s = \"aa\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:\n<\/strong>Both of the strings are a prefix of s. \nNote that the same string can occur multiple times in words, and it should be counted each time.<\/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;= 1000<\/code><\/li><li><code>1 &lt;= words[i].length, s.length &lt;= 10<\/code><\/li><li><code>words[i]<\/code>&nbsp;and&nbsp;<code>s<\/code>&nbsp;consist of lowercase English letters&nbsp;<strong>only<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n*m)<br>Space complexity: O(1)<\/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  int countPrefixes(vector<string>& words, string s) {\n    return count_if(begin(words), end(words), [&s](const string& word) {\n      return s.find(word) == 0;\n    });\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string array&nbsp;words&nbsp;and a string&nbsp;s, where&nbsp;words[i]&nbsp;and&nbsp;s&nbsp;comprise only of&nbsp;lowercase English letters. Return&nbsp;the&nbsp;number of strings&nbsp;in&nbsp;words&nbsp;that are a&nbsp;prefix&nbsp;of&nbsp;s. A&nbsp;prefix&nbsp;of a string is a substring that&#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":[222,4],"class_list":["post-9703","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9703","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=9703"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9703\/revisions"}],"predecessor-version":[{"id":9705,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9703\/revisions\/9705"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}