Skip to content

Commit e95292b

Browse files
authored
Problem Difficulty Level: Easy
1 parent a0063b3 commit e95292b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*--------------------------
2+
Time Complxity: O(n * L)
3+
Space Complexity: O(1)
4+
----------------------------*/
5+
6+
class Solution {
7+
public:
8+
bool matchPrefix(string &word, string &prefix){
9+
if(prefix.length() > word.length()) return false;
10+
int i = 0, j = 0;
11+
while(j < prefix.length() && word[i] == prefix[j]){
12+
i += 1;
13+
j += 1;
14+
}
15+
return j == prefix.length();
16+
}
17+
18+
int prefixCount(vector<string>& words, string pref) {
19+
int numMatches = 0;
20+
for(string &str : words){
21+
numMatches += matchPrefix(str, pref);
22+
}
23+
return numMatches;
24+
}
25+
};
26+
27+
/*
28+
Question Link: https://leetcode.com/problems/counting-words-with-a-given-prefix/
29+
Author: M.R.Naganathan
30+
*/

0 commit comments

Comments
 (0)