From 2d159f42638f87dfca97a0ab50099b0eef651e5e Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Thu, 9 Jan 2025 09:37:42 +0100 Subject: 2185. counting words with a given prefix --- counting_words_given_prefix.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 counting_words_given_prefix.py (limited to 'counting_words_given_prefix.py') diff --git a/counting_words_given_prefix.py b/counting_words_given_prefix.py new file mode 100644 index 0000000..49dbdab --- /dev/null +++ b/counting_words_given_prefix.py @@ -0,0 +1,27 @@ +# 2185. Counting Words With a Given Prefix + +""" +you are given an array of strings 'words' and a string 'pref'. return the +number of strings in 'words' that contain 'pref' as a prefix. a prefix of a +string 's' is any leading contiguous substring of 's'. +""" + + +class Solution(object): + def prefixCount(self, words, pref): + """ + :type words: List[str] + :type pref: str + :rtype: int + """ + cnt = 0 + for i in words: + if i.startswith(pref): + cnt += 1 + return cnt + + +if __name__ == "__main__": + obj = Solution() + print(obj.prefixCount(words=["pay", "attention", "practice", "attend"], pref="at")) + print(obj.prefixCount(words=["leetcode", "win", "loops", "success"], pref="code")) -- cgit v1.2.3