Skip to content

Commit

Permalink
Create count-prefixes-of-a-given-string.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored May 2, 2022
1 parent df6edd8 commit 3f599fe
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions C++/count-prefixes-of-a-given-string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Time: O(n * l)
// Space: O(1)

// string
class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
const auto& startsWith = [](const auto& s, const auto& prefix) {
return (size(prefix) <= size(s) && s.compare(0, size(prefix), prefix, 0, size(prefix)) == 0);
};
return accumulate(cbegin(words), cend(words), 0,
[&](const auto& total, const auto& x) {
return total + startsWith(s, x);
});
}
};

0 comments on commit 3f599fe

Please sign in to comment.