[解題] 890. Find and Replace Pattern

作者: SuicideComet (|)   2022-07-29 16:19:59
890. 尋找並取代
收到一個 List<string> words 和 string pattern
回傳一個 List<string>, 包含所有符合 pattern 的 words 元素
例如:
words = {"dff","gkk","ccc"}
pattern = "abb"
則回傳 {"dff", "gkk"}
https://i.imgur.com/yI62qoe.png
總覺得以前好像寫過類似的題目, 好像跟狗還貓有關的
這題我本來以為用兩個 dictionary 先 naive 好了
結果看大家 solution 好像跟這個複雜度都差不多
一開始很直觀 以為一個 dictionary 就夠了
只要記一個 dictionary<char, char>, key 是 pattern 的 char 而 value 是 word
也就是說建立起 pattern a => word d
pattern b => word f
這種對照表
再逐一檢查 word 的每個字元,就可以了
但後來發現假如 pattern 是 abc
而 word 是 ccc
這樣的話會變成 a => c, b => c, c => c
都判定 ok 所以就會錯誤
最後才乖乖建立兩個字典當雙向對照表,
總覺得有點奢侈

Links booklink

Contact Us: admin [ a t ] ucptt.com