Today’s problem
https://leetcode.com/problems/counting-words-with-a-given-prefix/description
Intuition
Do what the question ask!
Approach
The question yesterday can give us some insight of how to solve this question with minimal code.
Complexity
-
Time complexity: $O(N\times L)$, N is the length of the words, L is the length of a single word.
-
Space complexity: $O(1)$, only some variables are stored.
Code
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
ans = 0
for word in words:
if word.startswith(pref):
ans += 1
return ans