As it often goes with these types of interview questions, there's a lot of context missing. What is the goal? Do we want readable code? Do we want fast code? Are we constrained somehow? It seems here the author doesn't really know, but kudos to them for examining their assumptions.
As a side note, a trie would be a neat data structure to use in a solution for this toy problem.
As a side note, a trie would be a neat data structure to use in a solution for this toy problem.