Segment a word
Return whether text can be segmented into one or more nonempty dictionary words. The empty text is segmentable. Dictionary words are nonempty.
text = 'applepen'
reachable = 5
word = 'pen'
print(text.startswith(word, reachable))Reason about the state
Mark each reachable prefix length and extend it with matching dictionary words. A boolean table avoids recomputing the same suffix decisions. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return whether text can be segmented into one or more nonempty dictionary words. The empty text is segmentable. Dictionary words are nonempty.
The explanation opens here once every check passes.