Easy Solution | Beat 100% | c++ Solution
IntuitionThe problem asks us to count how many words in the given list words are prefixes of the string s. The simplest approach is to check each word in the li
# Intuition  
The problem asks us to count how many words in the given list `words` are prefixes of the string `s`. The simplest approach is to check each word in the list and see if it matches the prefix of `s`. If the word is a prefix, we increment the count.

---

# Approach  
1. **Initialize a count variable** to keep track of the number of prefixes found.  
2. **Iterate through the `words` vector**:
   - For each word, compare it with the corresponding prefix of `s` using `substr(0, word.length())`.
   - If the prefix matches the word, increment the `count` variable.  
3. **Return the `count`** after iterating through all words.

---

# Complexity

- **Time Complexity**:  
  Let \( n \) be the length of `s` and O\( m \) be the average length of the words in `words`.  
  For each word in `words`, we extract the prefix of length equal to the word's length, and the comparison operation takes linear time. Hence, the time complexity is \( O( w ) \), where \( w \) is the number of words in `words`.

- **Space Complexity**:  
  The space complexity is \( O(1) \) since we're only using a few extra variables for counting and string comparisons, regardless of the size of the input.

---

# Code
```cpp
class Solution {
public:
    // Function to count how many words in the 'words' vector are prefixes of the string 's'
    int countPrefixes(vector<string>& words, string s) {
        int count = 0; // Initialize the count of prefix matches
        
        // Loop through each word in the 'words' vector
        for (int i = 0; i < words.size(); i++) {
            // Extract the prefix of 's' with the same length as the current word
            string temp = s.substr(0, words[i].length());

            // If the prefix matches the current word, increment the count
            if (temp == words[i]) {
                count++;
            }
        }
        
        // Return the total count of prefixes found
        return count;
    }
};


['Array', 'String', 'String Matching', 'C++']