You can build the trie in a smarter way to cut the cost per search. Basically, when you index a string s of length n, instead of indexing itself , you index n+1 strings: the original string and n other strings with the character at each position missing. For instance, to index “abc”, you index all of the following: “abc” “bc” “ac” “ab" The overall index cost could be increased to O(N*k) in the worst case, where N is the total number of characters occurring in all the strings, and k is the average length of the strings. But when querying, you just need to do a straightforward search by skipping the “*” that is encountered, if at all.