Beats 100% of users with C++ || Step By Step Explain || Easy to Understand ||
Abhiraj pratap Singh\n\n---\n\n# UPVOTE \n\n----\n\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- The given code seems to be de
# Abhiraj pratap Singh\n\n---\n\n# UPVOTE \n\n----\n\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The given code seems to be defining a class Solution with two functions: isPrefix and prefixCount. The isPrefix function checks if a given string pref is a prefix of another string s, and the prefixCount function counts the number of strings in a vector words that have the specified prefix pref.\n\n---\n\n![Screenshot (47).png](https://assets.leetcode.com/users/images/fa9c4313-c0ae-4162-96d0-44f4c4e2487d_1706419130.0879095.png)\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1. The isPrefix function iterates through the characters of both strings (s and pref) and checks if they match for the first m characters (where m is the length of the prefix pref).\n2. The prefixCount function uses the isPrefix function to iterate through the vector of words (words) and increments the count whenever a word has the specified prefix.\n\n\n---\n\n\n# Complexity\n\n- **Time complexity:** Let\'s analyze the time complexity of each function:\n    - For the isPrefix function, the time complexity is O(m) where \'m\' is the length of the prefix string.\n    - For the prefixCount function, it iterates through each word in the vector, and for each word, it calls the isPrefix function. Therefore, the overall time complexity is O(n * m), where \'n\' is the number of words in the vector and \'m\' is the length of the prefix string.\n\n- **Space complexity:** The space complexity is O(1) for both functions as they use a constant amount of extra space regardless of the input size.\n\n\n---\n\n\n# Code\n```\nclass Solution {\npublic:\n    bool isPrefix(string s,string pref)\n    {\n        int n=s.length();\n        int m=pref.length();\n        if(n<m)\n            return false;\n        for(int i=0;i<m;i++)\n        {\n            if(pref[i]!=s[i])\n                return false;\n        }\n        return true;\n    }\n    int prefixCount(vector<string>& words, string pref) \n    {\n        int count=0;\n        for(int i=0;i<words.size();i++)\n        {\n            if( isPrefix(words[i],pref) )\n                count++;\n        }\n        return count;\n    }\n};\n```\n\n---\n\n# if you like the solution please UPVOTE it\n\n---\n\n![discord-discordgifemoji.gif](https://assets.leetcode.com/users/images/3724c92f-dd04-4738-b84f-d07622e5ee85_1706419343.2128263.gif)\n\n![fucking-die-redditor-stupid-cuck-funny-cat.gif](https://assets.leetcode.com/users/images/20ab3747-5112-4f1a-ad67-6da96f53b4eb_1706419345.5493877.gif)\n\n---
['Array', 'String', 'String Matching', 'C++']