import java.util.Arrays;
import java.util.List;
class WordBreakProblem
{
// Function to segment given String into a space-separated
// sequence of one or more dictionary words
public static void wordBreak(List
{
// if we have reached the end of the String,
// print the output String
if (str.length() == 0)
{
System.out.println(out);
return;
}
/*else {
System.out.println("Cant Break");
}*/
for (int i = 1; i <= str.length(); i++)
{
// consider all prefixes of current String
String prefix = str.substring(0, i);
// if the prefix is present in the dictionary, add prefix to the
// output String and recur for remaining String
if (dict.contains(prefix)) {
wordBreak(dict, str.substring(i), out + " " + prefix);
}
}
}
// main function
public static void main(String[] args)
{
// List of Strings to represent dictionary
List
"Word", "break", "b", "r", "e", "a", "k",
"br", "bre", "brea", "ak", "problem");
List
// input String
String str = "IamSumit";
wordBreak(dict1, str, "");
}
}
Comments
Post a Comment