LeetCode題解(1408):數組中的字符串匹配(Python)

題目:原題鏈接(簡單)

可應用字典樹

解法 時間複雜度 空間複雜度 執行用時
Ans 1 (Python) O(N2)O(N^2) O(1)O(1) 40ms (91.37%)
Ans 2 (Python)
Ans 3 (Python)

LeetCode的Python執行用時隨緣,只要時間複雜度沒有明顯差異,執行用時一般都在同一個量級,僅作參考意義。

解法一(暴力解法):

def stringMatching(self, words: List[str]) -> List[str]:
    ans = []
    for i1 in range(len(words)):
        word1 = words[i1]
        for i2 in range(len(words)):
            word2 = words[i2]
            if i1 != i2 and word1 in word2:
                ans.append(word1)
                break
    return ans
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章