A table that maps a single character to the set of patterns that begin with that character gives an order of magnitude improvement. Implement a version of i sspam that uses two characters as the index. How much improvement does that lead to? Thcsc arc simple special cases of a data structure called a trie. Most such data structures are based on trading space for time.