This problem needs trie data structure