Counting Words With a Given Prefix LeetCode solution in C++
1// The easiest solution to this problem is:2// Step 1: Calculate length of "pref"3// Step 2: find substring of "words" from 0 to length of "pref"4// if both match increment count by 1;56class Solution {7public:8int prefixCount(vector<string> &words, string pref) {9int count = 0;10int preflen = pref.size(); // step 11112for (auto i : words) {13if (i.substr(0, preflen) == pref) // step 214count++; // if both matches then increment count by 115}16return count; // return count17}18};