Using Trie here will be faster and less space consuming. If you know you are using mostly Engilsh letter you can make it even faster by using simple char[26] (for storing the nodes) while building the trie. So in the trie you will not have to build and store the hash.
Another solution would be:
ReplyDeleteUsing Trie here will be faster and less space consuming. If you know you are using mostly Engilsh letter you can make it even faster by using simple char[26] (for storing the nodes) while building the trie. So in the trie you will not have to build and store the hash.