2255. Count Prefixes of a Given String

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int countPrefixes(vector<string>& words, string s) {
int count = 0 ;

for(int i=0;i<s.size();++i){
string temp = s.substr(0,i+1);
for(const auto word:words){
if(word == temp) count++;
}
}
return count;
}
};

analysis

  • time complexity O(nm)
  • space complexity O(n)