【Leetcode】python - [2185] Counting Words With a Given Prefix 個人解法筆記 (內含範例程式碼)

題目出處

weekly-contest-282

2185. Counting Words With a Given Prefix

難度

Easy

個人範例程式碼

class Solution:
    def prefixCount(self, words: List[str], pref: str) -> int:
        pref_length = len(pref)
        ans_cnt = 0
        for ele in words:
            if len(ele) < pref_length:
                continue
            else:
                if self.compare_str(ele[0: pref_length], pref):
                    ans_cnt += 1
                else:
                    pass
        return ans_cnt

    def compare_str(self, word_pref: str, pref: str) -> bool:
        return (word_pref == pref)

Time Complexity

O(n)

算法說明

沒什麼,就取子字串直接比較

corner case 特殊情況處理

x

Boundary conditions/ Edge conditions 邊際情況處理

  • python 子字串長度

str[0:n] 取得範圍為 str 的 index 0 到 index n-1(包含)

Reference

Licensed under CC BY-NC-SA 4.0