211. Add and Search Word - Data structure design
Last updated
Last updated
Design a data structure that supports the following two operations:
search(word) can search a literal word or a regular expression string containing only letters a-z
or .
. A .
means it can represent any one letter.
Example:
Note:
You may assume that all words are consist of lowercase letters a-z
.
建立一個 collections class 中的 defaultdict() ,命名為 word_store
[addWord] --> 依據字串長度來分 list 存: [ ( 2, ["ab", "dv"] ), ( 1, ["a","b","c"] ), ( 6, [ "asdfgh"] ) ]
[search] -->
a) 如果該 search 字串沒有 “.” ,直接找 word_store 中該長度的字串有沒有符合的 b) 如果有 “.”,一個字母一個字母去比對該長度 list 中的字詞