First solution not using Trie