{"id":9506,"date":"2022-02-26T21:10:38","date_gmt":"2022-02-27T05:10:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9506"},"modified":"2022-02-26T21:11:07","modified_gmt":"2022-02-27T05:11:07","slug":"leetcode-2185-counting-words-with-a-given-prefix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2185-counting-words-with-a-given-prefix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2185. Counting Words With a Given Prefix"},"content":{"rendered":"\n<p>You are given an array of strings&nbsp;<code>words<\/code>&nbsp;and a string&nbsp;<code>pref<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of strings in&nbsp;<\/em><code>words<\/code><em>&nbsp;that contain&nbsp;<\/em><code>pref<\/code><em>&nbsp;as a&nbsp;<strong>prefix<\/strong><\/em>.<\/p>\n\n\n\n<p>A&nbsp;<strong>prefix<\/strong>&nbsp;of a string&nbsp;<code>s<\/code>&nbsp;is any leading contiguous substring of&nbsp;<code>s<\/code>.<\/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 = [\"pay\",\"<strong><u>at<\/u><\/strong>tention\",\"practice\",\"<strong>at<\/strong>tend\"], <code>pref <\/code>= \"at\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The 2 strings that contain \"at\" as a prefix are: \"<strong>at<\/strong>tention\" and \"<strong>at<\/strong>tend\".\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> words = [\"leetcode\",\"win\",\"loops\",\"success\"], <code>pref <\/code>= \"code\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no strings that contain \"code\" as a prefix.\n<\/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, pref.length &lt;= 100<\/code><\/li><li><code>words[i]<\/code>&nbsp;and&nbsp;<code>pref<\/code>&nbsp;consist of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Straight forward <\/strong><\/h2>\n\n\n\n<p>We can use std::count_if and std::string::find.<\/p>\n\n\n\n<p>Time complexity: O(n*l)<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 prefixCount(vector<string>& words, string pref) {\n    return count_if(begin(words), end(words), [&](const string& w) {\n      return w.find(pref) == 0;\n    });\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of strings&nbsp;words&nbsp;and a string&nbsp;pref. Return&nbsp;the number of strings in&nbsp;words&nbsp;that contain&nbsp;pref&nbsp;as a&nbsp;prefix. A&nbsp;prefix&nbsp;of a string&nbsp;s&nbsp;is any leading contiguous substring of&nbsp;s. Example&#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":[756,222,595,4],"class_list":["post-9506","post","type-post","status-publish","format-standard","hentry","category-string","tag-count_if","tag-easy","tag-find","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9506","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=9506"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9506\/revisions"}],"predecessor-version":[{"id":9508,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9506\/revisions\/9508"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}