We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a0063b3 commit e95292bCopy full SHA for e95292b
Strings/2185. Counting Words With a Given Prefix.cpp
@@ -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