Counting words with a given prefix

November 5, 2022

array-and-hashmap

Problem URL: Counting words with a given prefix

We will iterate over the words and count the number of words that start with the given prefix.

class Solution:
    def prefixCount(self, words: List[str], pref: str) -> int:
        res = 0
        for word in words:
            res += 1 if word.startswith(pref) else 0
        return res

Time Complexity: O(n)
Space Complexity: O(1)