# @hash table 技巧 # What are the differences between a HashMap and a Hashtable Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values. # hash table > if i not in dic 就是hash table 的精髓,因為他去查表,在O(1)的時間內查了所有的值。 下面就是建立一個hashmap 技巧,我們可以把一個串列或是list 或是string 所有出現的element和出現次數 用hash map 記錄下來(element 本身當key,出現次數當value ) ```python= s="strrt" dic={} for i in s: if i not in dic: dic[i]=1 else: dic[i]+=1 ``` 使用範例 242. Valid Anagram https://leetcode.com/problems/valid-anagram/ 上面的也可以用build in function 簡寫 用count( ) fucntion ```python= s="strrt" s_set=set(s) s.count(s_set) ```