C++ Easy Solution using Only One For loop and One If Condition
Intuition\nThe intuition for the problem is to iterate through each word in the words array, extract a substring from s that matches the length of the word, and
# Intuition\nThe intuition for the problem is to iterate through each word in the words array, extract a substring from s that matches the length of the word, and compare it with the word itself. Increment a counter each time a match is found, returning the final count of matching prefixes.\n\n# Approach\n**Initialize Counter :** Start by initializing a counter c to zero. This will keep track of the number of words that are prefixes of s.\n\n**Iterate through Words :** Loop through each word in the words array.\n\n**Check Prefix :**\n- For each word, determine its length n.\n- Extract the first n characters of the string s using substr(0, n).\n- Compare this substring with the word. If they are equal, increment the counter c.\n\n**Return Result :** After checking all the words, return the counter c as the final result.\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution {\npublic:\n    int countPrefixes(vector<string>& words, string s) {\n        int c=0;\n        for(int i=0;i<words.size();i++){\n            int n=words[i].size();\n            string a=s.substr(0,n);\n            if (words[i]==a) c++;\n        }\n        return c;\n        \n    }\n};\n```
['String', 'C++']