<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>count_if Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/count_if/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/count_if/</link>
	<description></description>
	<lastBuildDate>Sun, 27 Feb 2022 05:11:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>count_if Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/count_if/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2185. Counting Words With a Given Prefix</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-2185-counting-words-with-a-given-prefix/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-2185-counting-words-with-a-given-prefix/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 27 Feb 2022 05:10:38 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[count_if]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9506</guid>

					<description><![CDATA[<p>You are given an array of strings&#160;words&#160;and a string&#160;pref. Return&#160;the number of strings in&#160;words&#160;that contain&#160;pref&#160;as a&#160;prefix. A&#160;prefix&#160;of a string&#160;s&#160;is any leading contiguous substring of&#160;s. Example&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2185-counting-words-with-a-given-prefix/">花花酱 LeetCode 2185. Counting Words With a Given Prefix</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given an array of strings&nbsp;<code>words</code>&nbsp;and a string&nbsp;<code>pref</code>.</p>



<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>



<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>



<p><strong>Example 1:</strong></p>



<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"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The 2 strings that contain "at" as a prefix are: "<strong>at</strong>tention" and "<strong>at</strong>tend".
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> words = ["leetcode","win","loops","success"], <code>pref </code>= "code"
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no strings that contain "code" as a prefix.
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Straight forward </strong></h2>



<p>We can use std::count_if and std::string::find.</p>



<p>Time complexity: O(n*l)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int prefixCount(vector&lt;string&gt;&amp; words, string pref) {
    return count_if(begin(words), end(words), [&amp;](const string&amp; w) {
      return w.find(pref) == 0;
    });
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-2185-counting-words-with-a-given-prefix/">花花酱 LeetCode 2185. Counting Words With a Given Prefix</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-2185-counting-words-with-a-given-prefix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1967. Number of Strings That Appear as Substrings in Word</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-1967-number-of-strings-that-appear-as-substrings-in-word/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-1967-number-of-strings-that-appear-as-substrings-in-word/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 31 Dec 2021 22:26:30 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[count_if]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9339</guid>

					<description><![CDATA[<p>Given an array of strings&#160;patterns&#160;and a string&#160;word, return&#160;the&#160;number&#160;of strings in&#160;patterns&#160;that exist as a&#160;substring&#160;in&#160;word. A&#160;substring&#160;is a contiguous sequence of characters within a string. Example 1: Input:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1967-number-of-strings-that-appear-as-substrings-in-word/">花花酱 LeetCode 1967. Number of Strings That Appear as Substrings in Word</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an array of strings&nbsp;<code>patterns</code>&nbsp;and a string&nbsp;<code>word</code>, return&nbsp;<em>the&nbsp;<strong>number</strong>&nbsp;of strings in&nbsp;</em><code>patterns</code><em>&nbsp;that exist as a&nbsp;<strong>substring</strong>&nbsp;in&nbsp;</em><code>word</code>.</p>



<p>A&nbsp;<strong>substring</strong>&nbsp;is a contiguous sequence of characters within a string.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> patterns = ["a","abc","bc","d"], word = "abc"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- "a" appears as a substring in "abc".
- "abc" appears as a substring in "abc".
- "bc" appears as a substring in "abc".
- "d" does not appear as a substring in "abc".
3 of the strings in patterns appear as a substring in word.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> patterns = ["a","b","c"], word = "aaaaabbbbb"
<strong>Output:</strong> 2
<strong>Explanation:</strong>
- "a" appears as a substring in "aaaaabbbbb".
- "b" appears as a substring in "aaaaabbbbb".
- "c" does not appear as a substring in "aaaaabbbbb".
2 of the strings in patterns appear as a substring in word.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> patterns = ["a","a","a"], word = "ab"
<strong>Output:</strong> 3
<strong>Explanation:</strong> Each of the patterns appears as a substring in word "ab".
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= patterns.length &lt;= 100</code></li><li><code>1 &lt;= patterns[i].length &lt;= 100</code></li><li><code>1 &lt;= word.length &lt;= 100</code></li><li><code>patterns[i]</code>&nbsp;and&nbsp;<code>word</code>&nbsp;consist of lowercase English letters.</li></ul>



<h2><strong>Solution: Brute Force</strong></h2>



<p>We can use <a href="https://en.cppreference.com/w/cpp/algorithm/count" target="_blank" rel="noreferrer noopener">count_if</a> for 1-liner.</p>



<p>Time complexity: O(m*n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numOfStrings(vector&lt;string&gt;&amp; patterns, string word) {
    return count_if(begin(patterns), end(patterns), [&amp;](string&amp; p) {
      return word.find(p) != string::npos;
    });
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-1967-number-of-strings-that-appear-as-substrings-in-word/">花花酱 LeetCode 1967. Number of Strings That Appear as Substrings in Word</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-1967-number-of-strings-that-appear-as-substrings-in-word/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
