java - Use Hash Table to create a dictionary of words -
i studying, self, hash tables using following course: http://algs4.cs.princeton.edu/34hash/
at exercises part, i've found followig:
password checker. write program reads in string command line , dictionary of words standard input, , checks whether "good" password. here, assume "good" means (i) @ least 8 characters long, (ii) not word in dictionary, (iii) not word in dictionary followed digit 0-9 (e.g., hello5), (iv) not 2 words separated digit (e.g., hello2world)
i think confused how use hash table (hashmap). suppose easier exercise: need check if word @ dictionary , need using hash table. guess should add words in dictionary using word key and, if want check if given word @ dictionary, use "get" method. if found, word not password. but:
1) should value have put associated given key?
2) if 2 words hash same place? know collision part solved using linear probing or separate chaining, when use get, handled in data structure?
i don't want write code, trying understand how works.
thanks in advance!
@edit: idea had make use of hashcode. suppose have array of strings words of dictionary. then, if have same hash code, must compare (since hash must consistent equals). if understood well, value doesn't matter, in case, should check if word @ dictionary. should check if returned me something.
1) should value have put associated given key?
if @ java hashset
implementation, see internally uses hashmap
, items added map keys, , value dummy object, shared entries. dictionary keys structure more hashset
hashmap
, if have no specific value (like popularity example) associate key.
2) if 2 words hash same place? know collision part solved using linear probing or separate chaining, when use get, handled in data structure?
java hashmap
implementation uses separate chaining, items same hash code put in linked list structure. not have worry collision resolving, when use hashmap (unless goal prevent hash attacks).
Comments
Post a Comment