Link: https://leetcode.com/problems/word-break/
Solution:
Topics: BFS, DP, Word break II
Intuition
My first solution was actually DP similar to Word break II, but the most efficient solution is actually BFS. I think its worth going through the BFS solution because its very elegant, yet very subtle mainly because DP is the more obvious path.
The key intuition for the BFS solution is realizing that (unlike Word break II), all we are asked is can the word be broken up or not. There are potentially many ways to break up the string, and the recursive DP solution (like in Word break II) would be forced to explore all of them. But the problem only asks for one, so if we frame this as a shortest path problem (break the string into the least amount of words), then we can skip exploring ALL the ways the string can be broken and just early exit our BFS (return True), when a way to break has been found.
Implementation (BFS)
This is the optimized DP solution with only one cached variable (as opposed to 2 in Word break II). The time and memory complexity is the same as the BFS solution, but BFS is still a bit better because I think in the DP case there is no guarantee that the “way” found will be the shortest one.
Implementation (DP)
Visual