14/35
Part 4 of 7 · Walking alongsideLearn and do

Walk the trie from each start the table reached, and stop when no word goes on

A trie lets the same table skip the slices altogether. Call the new version can_split(text, trie). It keeps the ok table, but its outer loop runs over starts instead of ends.

A start that ok hasn't reached is skipped: no split gets there, so no word can start there. From a start it has reached, walk the trie along the text one letter at a time, the way lookup does. Step to the child for text[end], and stop the moment there's no child, because no word goes on. Wherever the node you reach holds the end marker, a word ends after text[end], so set ok[end + 1] to True.

On the gear words it gives True for tentspole and False for tentpol, the same answers as the slices.

To count the splits instead, keep a number in each cell. ways[i] is how many ways text[:i] splits, with ways[0] = 1 for the empty split. Where the yes-or-no version sets True, add ways[start] to the cell past the word's end.

Predict

On tentspole, the walk from start 0 goes t, e, n, t, s, and then meets p. Which ok entries does it mark?

Pick an answer above to see the rest