LeetCode 140: Word Break II
Problem Description
Explanation
To solve this problem, we can use dynamic programming along with backtracking. We will first check if the input string can be segmented using the words in the dictionary. Then, we will find all possible sentences by backtracking through the segmented words.
-
Dynamic Programming:
- We will use a boolean array
dp[]
of sizen+1
wheren
is the length of the input strings
.dp[i]
will be true if the substrings[0:i]
can be segmented using the words in the dictionary. - We will initialize
dp[0]
as true to indicate an empty string can be segmented. - We will iterate through each index
i
from 1 ton
and check if there exists an indexj
such thatdp[j]
is true ands.substring(j, i)
is in the dictionary. If both conditions are met, we will setdp[i]
as true.
- We will use a boolean array
-
Backtracking:
- After finding all possible segmented positions using dynamic programming, we will perform backtracking to construct all valid sentences.
- We will maintain a list of strings
result
to store all valid sentences. - Starting from the end of the input string, we will backtrack through the segmented positions to construct each valid sentence.
- We will recursively explore all possible combinations of words from the dictionary to form valid sentences.
-
Time Complexity:
- The time complexity of this approach is O(n^2 * m) where
n
is the length of the input strings
andm
is the average length of words in the dictionary.
- The time complexity of this approach is O(n^2 * m) where
-
Space Complexity:
- The space complexity is O(n) for the boolean array
dp
and O(n * m) for the backtracking stack.
- The space complexity is O(n) for the boolean array
Solutions
class Solution {
public List<String> wordBreak(String s, List<String> wordDict) {
List<String> result = new ArrayList<>();
Set<String> dict = new HashSet<>(wordDict);
int n = s.length();
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && dict.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
if (dp[n]) {
backtrack(s, n, dict, new ArrayList<>(), result);
}
return result;
}
private void backtrack(String s, int end, Set<String> dict, List<String> path, List<String> result) {
if (end == 0) {
result.add(String.join(" ", path));
return;
}
for (int i = end - 1; i >= 0; i--) {
String word = s.substring(i, end);
if (dict.contains(word)) {
path.add(0, word);
backtrack(s, i, dict, path, result);
path.remove(0);
}
}
}
}
Related LeetCode Problems
Loading editor...