2185. Counting Words With a Given Prefix

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int prefixCount(vector<string>& words, string pref) {
int n = pref.size(), count = 0;
for(string word:words){
int i=0;
for(char c:word){
if(c!=pref[i]) break;
else i++;
}
if(i==n) count++;
}
return count;
}
};

analysis

  • time complexity O(n+m)
  • space complexity O(1)