-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path9_Counting_Words_With_a_Given_Prefix.cpp
73 lines (57 loc) · 2.01 KB
/
9_Counting_Words_With_a_Given_Prefix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 2185. 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:
// 1 <= words.length <= 100
// 1 <= words[i].length, pref.length <= 100
// words[i] and pref consist of lowercase English letters.
class Solution
{
public:
bool find(string s, string pref)
{
for (int i = 0; i < pref.length(); i++)
{
if (s[i] != pref[i])
{
return false;
}
}
return true;
}
int prefixCount(vector<string> &words, string pref)
{
int ans = 0;
for (auto &w : words)
{
if (find(w, pref))
ans++;
}
return ans;
}
};
/*
This code solves the problem of counting words with a given prefix. Here's how it works:
1. The find() function:
- Takes two strings as input: the word to check (s) and the prefix to find (pref)
- Compares each character of the word with the prefix
- Returns false if any character doesn't match
- Returns true if all characters in prefix match with the word's beginning
2. The prefixCount() function:
- Takes a vector of strings (words) and a prefix string (pref)
- Initializes a counter variable 'ans'
- Iterates through each word in the vector
- Uses find() function to check if the word contains the prefix
- Increments counter if prefix is found
- Returns the final count
The solution has a time complexity of O(N * M) where N is the number of words and M is the length of the prefix.
*/