139 Word Break
Last updated
Last updated
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
Return true because "leetcode" can be segmented as "leet code".
The Idea: The goal is to find a continuous set of substrings in the dictionary that can build S. One way of doing this is through a graph model. The graph itself will represent all the substrings path that exists within the dictionary to build S.
Consider the following example:
Like many algorithm questions, we can model this problem using a graph. In the context of this graph, our goal is to look for a viable path that exists within the dictionary from the parent index that creates a continuous representation of our string S
. In the example above, our possible options beginning at index 0 are (night - 5), (nigh - 4), or (ni - 2). From each of these options, we explore a smaller, but identical subproblem.
Of course that can be more than one possible path that in the tree that leads to an appropriate answer. However, we are only interested IF it is possible.
A BFS procedure would allow us to quickly identify the shortest path. A DFS procedure may otherwise explore down a massive subtree that doesnt contain the ending index.
In the graph, we notice some repeating subtrees. This happens because two words in our dictionary can addup to the same index. For example, in the example above, consider the indicies 4 (nigh) and 5 (night). Following (nigh) we can take dictionary word (ty) and obtain index 6 (nighty). At the same time, (night) can take (y) to obtain the same index 6 (nighty).
In any case, we want to avoid any redundant computations, and only perform the computation down the tree once. To solve this, we maintain a visited array that tells us which indices have been explored. Doing this removes duplicate subtrees:
Complexity: O(N^3) time where N is the length of the string. We have to try and identify every substring in S, and see if it is in the dictionary which takes (n-1)+(n-2)+(n-3)... or N^2 time due to string comparison and hash. This is explored for every index of S, which is only iterated through once due to the visited set.
Might be N^2 if substring check is O(1) per string
Testing
Python