Word Break

medium
Unit 11 · Dynamic Programming I0/2 to milestone

Can a string be split into a sequence of dictionary words? Think splitting hashtags ("machinelearning" → "machine learning") or segmenting text in languages without spaces.

Implement can_break(s, words) and segment(s, words), which returns one valid segmentation as a list, preferring the split whose first word is longest, then recursively the same rule. Return None if there isn't one. Words can be reused.

#dynamic-programming#strings2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Word Break · TechGradify