LeetCode 139 - Word Break I
LeetCode 140 - Word Break II
Dynamic Programming | Set 32 (Word Break Problem) | GeeksforGeeks
Given an input string and a dictionary of words, find out if the input string can be segmented into a space-separated sequence of dictionary words.
Dynamic Programming
we consider each prefix and search it in dictionary. If the prefix is present in dictionary, we recur for rest of the string (or suffix). If the recursive call for suffix returns true, we return true, otherwise we try next prefix. If we have tried all prefixes and none of them resulted in a solution, we return false.
Recursive Version:
EPI Java Solution
// T[i] stores the length of the last string which composed of s(0, i).
int[] T = new int[s.length()];
for (int i = 0; i < s.length(); ++i) {
// Sets T[i] if s(0, i) is a valid word.
if (dict.contains(s.substring(0, i + 1))) {
T[i] = i + 1;
}
// Set T[i] if T[j] != 0 and s(j + 1, i) is a valid word.
for (int j = 0; j < i && T[i] == 0; ++j) {
if (T[j] != 0 && (j + 1 < i - j)
&& dict.contains(s.substring(j + 1, i - j))) {
T[i] = i - j;
}
}
}
List<String> ret = new ArrayList<>();
// s can be assembled by valid words.
if (T[T.length - 1] != 0) {
int idx = s.length() - 1;
while (idx >= 0) {
ret.add(s.substring(idx - T[idx] + 1, idx + 1));
idx -= T[idx];
}
Collections.reverse(ret);
}
return ret;
}
http://ideone.com/53LMkr
A recursive program to print all possible partitions of a given string into dictionary words
int dictionaryContains(string word)
{
string dictionary[] = {"mobile","samsung","sam","sung","man","mango",
"icecream","and","go","i","love","ice","cream"};
int size = sizeof(dictionary)/sizeof(dictionary[0]);
for (int i = 0; i < size; i++)
if (dictionary[i].compare(word) == 0)
return true;
return false;
}
//prototype of wordBreakUtil
void wordBreakUtil(string str, int size, string result);
void wordBreak(string str)
{
//last argument is prefix
wordBreakUtil(str, str.size(), "");
}
// result store the current prefix with spaces between words
void wordBreakUtil(string str, int size, string result)
{
//Process all prefixes one by one
for (int i=1; i<=size; i++)
{
//extract substring from 0 to i in prefix
string prefix = str.substr(0, i);
// if dictionary conatins this prefix, then we check
// for remaining string. Otherwise we ignore this prefix
// (there is no else for this if) and try next
if (dictionaryContains(prefix))
{
// if no more elements are there, print it
if (i == size)
{
// add this element to previous prefix
result += prefix;
cout << result << endl; //print result
return;
}
wordBreakUtil(str.substr(i, size-i), size-i, result+prefix+" ");
}
} //end for
}//end function
http://baozitraining.org/blog/calculate-hard-runtime-complexity-in-recursive-solution/
Read full article from Dynamic Programming | Set 32 (Word Break Problem) | GeeksforGeeks
LeetCode 140 - Word Break II
Dynamic Programming | Set 32 (Word Break Problem) | GeeksforGeeks
Given an input string and a dictionary of words, find out if the input string can be segmented into a space-separated sequence of dictionary words.
Dynamic Programming
we consider each prefix and search it in dictionary. If the prefix is present in dictionary, we recur for rest of the string (or suffix). If the recursive call for suffix returns true, we return true, otherwise we try next prefix. If we have tried all prefixes and none of them resulted in a solution, we return false.
bool wordBreak(string str){ int size = str.size(); if (size == 0) return true; // Create the DP table to store results of subroblems. The value wb[i] // will be true if str[0..i-1] can be segmented into dictionary words, // otherwise false. bool wb[size+1]; memset(wb, 0, sizeof(wb)); // Initialize all values as false. for (int i=1; i<=size; i++) { // if wb[i] is false, then check if current prefix can make it true. // Current prefix is "str.substr(0, i)" if (wb[i] == false && dictionaryContains( str.substr(0, i) )) wb[i] = true; // wb[i] is true, then check for all substrings starting from // (i+1)th character and store their results. if (wb[i] == true) { // If we reached the last prefix if (i == size) return true; for (int j = i+1; j <= size; j++) { // Update wb[j] if it is false and can be updated // Note the parameter passed to dictionaryContains() is // substring starting from index 'i' and length 'j-i' if (wb[j] == false && dictionaryContains( str.substr(i, j-i) )) wb[j] = true; // If we reached the last character if (j == size && wb[j] == true) return true; } } } /* Uncomment these lines to print DP table "wb[]" for (int i = 1; i <= size; i++) cout << " " << wb[i]; */ // If we have tried all prefixes and none of them worked return false;}Recursive Version:
bool wordBreak(string str){ int size = str.size(); // Base case if (size == 0) return true; // Try all prefixes of lengths from 1 to size for (int i=1; i<=size; i++) { // The parameter for dictionaryContains is str.substr(0, i) // str.substr(0, i) which is prefix (of input string) of // length 'i'. We first check whether current prefix is in // dictionary. Then we recursively check for remaining string // str.substr(i, size-i) which is suffix of length size-i if (dictionaryContains( str.substr(0, i) ) && wordBreak( str.substr(i, size-i) )) return true; } // If we have tried all prefixes and none of them worked return false;}The \textsf{bedbathandbeyond.com} problem WordBreaking.java
public static List<String> wordBreaking(String s, Set<String> dict) {// T[i] stores the length of the last string which composed of s(0, i).
int[] T = new int[s.length()];
for (int i = 0; i < s.length(); ++i) {
// Sets T[i] if s(0, i) is a valid word.
if (dict.contains(s.substring(0, i + 1))) {
T[i] = i + 1;
}
// Set T[i] if T[j] != 0 and s(j + 1, i) is a valid word.
for (int j = 0; j < i && T[i] == 0; ++j) {
if (T[j] != 0 && (j + 1 < i - j)
&& dict.contains(s.substring(j + 1, i - j))) {
T[i] = i - j;
}
}
}
List<String> ret = new ArrayList<>();
// s can be assembled by valid words.
if (T[T.length - 1] != 0) {
int idx = s.length() - 1;
while (idx >= 0) {
ret.add(s.substring(idx - T[idx] + 1, idx + 1));
idx -= T[idx];
}
Collections.reverse(ret);
}
return ret;
}
http://ideone.com/53LMkr
A recursive program to print all possible partitions of a given string into dictionary words
int dictionaryContains(string word)
{
string dictionary[] = {"mobile","samsung","sam","sung","man","mango",
"icecream","and","go","i","love","ice","cream"};
int size = sizeof(dictionary)/sizeof(dictionary[0]);
for (int i = 0; i < size; i++)
if (dictionary[i].compare(word) == 0)
return true;
return false;
}
//prototype of wordBreakUtil
void wordBreakUtil(string str, int size, string result);
void wordBreak(string str)
{
//last argument is prefix
wordBreakUtil(str, str.size(), "");
}
// result store the current prefix with spaces between words
void wordBreakUtil(string str, int size, string result)
{
//Process all prefixes one by one
for (int i=1; i<=size; i++)
{
//extract substring from 0 to i in prefix
string prefix = str.substr(0, i);
// if dictionary conatins this prefix, then we check
// for remaining string. Otherwise we ignore this prefix
// (there is no else for this if) and try next
if (dictionaryContains(prefix))
{
// if no more elements are there, print it
if (i == size)
{
// add this element to previous prefix
result += prefix;
cout << result << endl; //print result
return;
}
wordBreakUtil(str.substr(i, size-i), size-i, result+prefix+" ");
}
} //end for
}//end function
http://baozitraining.org/blog/calculate-hard-runtime-complexity-in-recursive-solution/
Read full article from Dynamic Programming | Set 32 (Word Break Problem) | GeeksforGeeks