Refactored trie to involve function GetOrCreate which helps caching prefixes.