Skip to content

Commit

Permalink
Create counting-words-with-a-given-prefix.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Feb 28, 2022
1 parent 9c7cc15 commit c407ca1
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions C++/counting-words-with-a-given-prefix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Time: O(n * p)
// Space: O(1)

// string
class Solution {
public:
int prefixCount(vector<string>& words, string pref) {
return count_if(cbegin(words), cend(words),
[&pref](const auto& x) {
return x.compare(0, size(pref), pref) == 0;
});
}
};

0 comments on commit c407ca1

Please sign in to comment.