-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_jan
33 lines (24 loc) · 775 Bytes
/
9_jan
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
/*
ou 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".
*/
//approch
class Solution {
public:
int prefixCount(vector<string>& words, string pref) {
int n = words.size();
int len = pref.length();
int cnt = 0;
for(int i = 0 ; i < n ; i++){
string str = words[i];
//if(str.find(pref) == 0) cnt++;
if(str.substr(0,len) == pref) cnt++;
}
return cnt;
}
};