Skip to content

Commit 3f599fe

Browse files
authored
Create count-prefixes-of-a-given-string.cpp
1 parent df6edd8 commit 3f599fe

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time: O(n * l)
2+
// Space: O(1)
3+
4+
// string
5+
class Solution {
6+
public:
7+
int countPrefixes(vector<string>& words, string s) {
8+
const auto& startsWith = [](const auto& s, const auto& prefix) {
9+
return (size(prefix) <= size(s) && s.compare(0, size(prefix), prefix, 0, size(prefix)) == 0);
10+
};
11+
return accumulate(cbegin(words), cend(words), 0,
12+
[&](const auto& total, const auto& x) {
13+
return total + startsWith(s, x);
14+
});
15+
}
16+
};

0 commit comments

Comments
 (0)