I suspect you'll want something a touch faster than O(l) for every lookup, in fact given the previous constraint of memory being no problem, you should instead use a hashtable.
Exactly. I was amazed at how little ground was covered by the end of the post, off on a yak shave for the part of this problem that is not hard or novel.