Beats 100% of users with C++ || Step By Step Explain || Easy to Understand ||
Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it....\n\n---\n\n# Intuition\n Describe your first thoughts on how to solve this problem
# Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it....\n\n---\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem involves counting the number of words in a vector that have a specific prefix equal to a given string s.\n\n---\n\n![Screenshot (47).png](https://assets.leetcode.com/users/images/5d488ce1-b1d1-4f4e-830c-5823a77958ac_1707146741.4763806.png)\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n- The class Solution contains a member function countPrefixes.\n- It iterates through each word in the vector.\n- Compares characters at corresponding positions in the word and the given string s.\n- If a mismatch is found or the end of the word is reached, move to the next word.\n- If the end of the word is reached, increment the count, indicating a match.\n\n\n---\n\n# Complexity\n\n- **Time complexity:** O(n*m), where n is the number of words and m is the length of the strings.\n\n- **Space complexity:** O(1), as the algorithm uses a constant amount of space regardless of the input size.\n\n\n---\n\n# Code\n```\nclass Solution {\npublic:\n    int countPrefixes(vector<string>& words, string s)\n    {\n        string w = "" ;\n        int count = 0 ;\n        for(int i=0 ; i<words.size() ; i++)\n        {\n            w = words[i] ;\n            int j ;\n            for(j=0 ; j<w.size() ; j++)\n            {\n                if(w[j]!=s[j])\n                    break ;\n            }\n            if(j==w.size())\n                count++ ;\n        }   \n        return count ;\n    }\n};\n```\n\n---\n# if you like the solution please UPVOTE it \n\n![discord-discordgifemoji.gif](https://assets.leetcode.com/users/images/a92ea0e3-dc70-4155-8a4a-8555b54d78b1_1707146872.872256.gif)\n\n![fucking-die-redditor-stupid-cuck-funny-cat.gif](https://assets.leetcode.com/users/images/33d4255a-cd43-42a6-88a2-39b061dbf086_1707146922.1656475.gif)\n\n----\n---\n
['Array', 'String', 'String Matching', 'C++']