24/35
Part 6 of 7 · The worked exampleLearn

Test the edges, then measure: at 100,000 words, building the trie took most of the time

5. Edge cases.

  • A word of one letter is found at the first cell with that letter.
  • The same word listed twice makes one path and one marker, so it's reported once.
  • A word longer than the number of cells can never be found, and the search finds that out without trying it specially.
  • The recursion goes as deep as the longest word, 24 letters in the big list below, so the recursion limit isn't a concern here.

6. Cost, measured. The grid, one row per string: ["hlxhhr", "uaarhc", "czathi", "tedgme", "ushmbc", "macede"]. The words are the first 1,000, 10,000 and 100,000 of the sample, one run each:

WordsOne search per wordOne trie
1,0009.7 ms0.9 ms
10,00086.8 ms8.6 ms
100,000861.1 ms172.5 ms

They found 0, 4 and 47 words.

The trie version's time is mostly building the trie: building it alone for the 100,000 words took 140.4 ms. Say that trade out loud.

If the same word list is searched on many grids, remember that pop and del change the trie. Rebuild it per grid, since a copy.deepcopy of it took about three times as long as a fresh build. Or drop both the pop and the del, and collect the found words in a set.