0% found this document useful (0 votes)
205 views

Word Break Problem Using Backtracking

The document describes the word break problem using backtracking. Given a sentence without spaces and a dictionary, the problem is to find all possible ways to break the sentence into words from the dictionary. The algorithm uses backtracking to recursively try different partitions of the input string and check if the partitions contain dictionary words. It demonstrates the problem and algorithm on sample inputs and outputs the possible word breaks. Pseudocode for the backtracking function and main function is provided to illustrate the recursive approach.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
205 views

Word Break Problem Using Backtracking

The document describes the word break problem using backtracking. Given a sentence without spaces and a dictionary, the problem is to find all possible ways to break the sentence into words from the dictionary. The algorithm uses backtracking to recursively try different partitions of the input string and check if the partitions contain dictionary words. It demonstrates the problem and algorithm on sample inputs and outputs the possible word breaks. Pseudocode for the backtracking function and main function is provided to illustrate the recursive approach.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Word Break problem using

Backtracking

PPT by,
Chaitanya Kumar P.A.S
What Is the Problem
Word Break Problem using
Backtracking
Given a valid sentence without any spaces between the
words and a dictionary of valid English words, find all possible
ways to break the sentence in individual dictionary words.

Example
Consider the following dictionary
{ i, like, sam, sung, samsung, mobile, ice,
cream, icecream, man, go, mango}

Input: "ilikesamsungmobile“
Output: i like sam sung mobile
i like samsung mobile

Input: "ilikeicecreamandmango"
Output: i like ice cream and man go
i like ice cream and mango
i like icecream and man go
i like icecream and mango
Why backtracking– Assume “ilovebacktrackingicecream”

string dictionary[] =
{"mobile","samsung","sam","sung",
"man","mango", "icecream","and",
"go","i","love","ice","cream","back","trac
k“,”backtrack”};

Here ,

We the out put must be


Dictionary
int dictionaryContains(string &word)
{

string dictionary[]=
{"mobile","samsung","sam","sung“,"man",
"mango",
"icecream","and","go","i","love","ice","cre
am"};

Int
n=sizeof(dictionary)/sizeof(dictionary[0]);

for (int i = 0; i < n; i++)


if (dictionary[i].compare(word) == 0)
return true;
return false;
}
Main()
int main()
{
cout << "First Test:\n";
wordBreak("ilovebacktrac
kicecream");

return 0;
}
Wordbreak(string str)
void wordBreak(string str)
{
// last argument is prefix
wordBreakUtil(str,str.size
(), "");

}
substr
wordbreakutil
void wordBreakUtil(string str, int n, string
result)
{
for (int i=1; i<=n; i++)
{
string prefix = str.substr(0, i);
if (dictionaryContains(prefix))
{
if (i == n)
{
result += prefix;
cout << result << endl; //print
result
return;
}
wordBreakUtil(str.substr(i, n-i), n-
i,result + prefix + " ");
}
}
}//end function
#include <iostream>
Program
using namespace std;
int dictionaryContains(string &word)
{
string dictionary[] = {"mobile","samsung","sam","sung", "man","mango", "icecream","and","go","i","love","ice","cream","back","track","backtrack"};
int n = sizeof(dictionary)/sizeof(dictionary[0]);
for (int i = 0; i < n; i++)
if (dictionary[i].compare(word) == 0)
return true;
return false;
}
void wordBreakUtil(string str, int size, string result);
void wordBreak(string str)
{
// last argument is prefix
wordBreakUtil(str, str.size(), "");
}
void wordBreakUtil(string str, int n, string result)
{

for (int i=1; i<=n; i++)


{

string prefix = str.substr(0, i);

if (dictionaryContains(prefix))
{

if (i == n)
{

result += prefix;
cout << result << endl; //print result
return;
}
wordBreakUtil(str.substr(i, n-i), n-i,
result + prefix + " ");
}
}
}
int main()
{
cout << "First Test:\n";
wordBreak("ilovebacktrackicecream");

return 0;

}
output
Thanking u ,
Chaitanya Kumar P.A.S

You might also like