From 6852eea40eeb63f20b158fdab230e92c39dfa7a1 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Sat, 26 Feb 2022 20:41:04 -0800 Subject: [PATCH] 2185 --- .../README.md | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 leetcode/2185. Counting Words With a Given Prefix/README.md diff --git a/leetcode/2185. Counting Words With a Given Prefix/README.md b/leetcode/2185. Counting Words With a Given Prefix/README.md new file mode 100644 index 00000000..39cb4c54 --- /dev/null +++ b/leetcode/2185. Counting Words With a Given Prefix/README.md @@ -0,0 +1,54 @@ +# [2185. Counting Words With a Given Prefix (Easy)](https://leetcode.com/problems/counting-words-with-a-given-prefix/) + +

You are given an array of strings words and a string pref.

+ +

Return the number of strings in words that contain pref as a prefix.

+ +

A prefix of a string s is any leading contiguous substring of s.

+ +

 

+

Example 1:

+ +
Input: words = ["pay","attention","practice","attend"], pref = "at"
+Output: 2
+Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
+
+ +

Example 2:

+ +
Input: words = ["leetcode","win","loops","success"], pref = "code"
+Output: 0
+Explanation: There are no strings that contain "code" as a prefix.
+
+ +

 

+

Constraints:

+ + + + +**Similar Questions**: +* [Check If a Word Occurs As a Prefix of Any Word in a Sentence (Easy)](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/) + +## Solution 1. + +```cpp +// OJ: https://leetcode.com/problems/counting-words-with-a-given-prefix/ +// Author: github.com/lzl124631x +// Time: O(NW) +// Space: O(N) +class Solution { +public: + int prefixCount(vector& A, string pref) { + int ans = 0; + for (auto &s : A) { + if (s.substr(0, pref.size()) == pref) ++ans; + } + return ans; + } +}; +``` \ No newline at end of file