題目:原題鏈接(簡單)
可應用字典樹
解法 | 時間複雜度 | 空間複雜度 | 執行用時 |
---|---|---|---|
Ans 1 (Python) | 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