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. See following examples for more details.
This is a famous Google interview question, also being asked by another other companies now a days.
Consider the following dictionary { i, like, sam, sung, samsung, mobile, ice, cream, icecream, man, go, mango}Input: ilikeOutput: Yes The string can be segmented as "i like".Input: ilikesamsungOutput: YesThe string can be segmented as "i like samsung" or "i like sam sung".
Recursive implementation:
The idea is simple, 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.
Changing DFS to DP can reduce the calculation workload.
Package DP; import java. util. arrayList; public class WordBreak {public static void main (String [] args) {System. out. println (wordBreakRec ("ilikesamsung"); System. out. println (wordBreakDP ("ilikesamdp"); wordBreakPrintAll ("ilikesamsung"); System. out. println (wordBreakRec ("samsungandmango"); System. out. println (wordBreakDP ("samsungandmango"); wordBreakPrintAll ("samsungandmango"); System. out. println (wordBreakRec ("samsungandmangok"); System. out. println (wordBreakDP ("samsungandmangok"); wordBreakPrintAll ("samsungandmangok");} public static boolean wordBreakRec (String s) {int len = s. length (); if (len = 0) {return true;} // DFS // Try all prefixes of lengths from 1 to sizefor (int I = 1; I <= len; I ++) {// The parameter for dictionaryContains is s. substring (0, I) // s. substring (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 // s. substring (I) which is suffix of length size-iif (dictionaryContains (s. substring (0, I) & wordBreakRec (s. substring (I) {return true ;}// If we have tried all prefixes and none of them workedreturn false;} // print all the combinations, to print out all the combinations, instead of determining whether the combinations can be used, you can only use dfspublic static void wordBreakPrintAll (String s) {ArrayList
Al = new ArrayList
(); WordBreakRec2 (s, al);} public static void wordBreakRec2 (String s, ArrayList
Al) {int len = s. length (); if (len = 0) {System. out. println (al); return;} // DFSfor (int I = 1; I <= len; I ++) {String substr = s. substring (0, I); if (dictionaryContains (substr) {al. add (substr); wordBreakRec2 (s. substring (I), al); al. remove (al. size ()-1) ;}} private static boolean dictionaryContains (String word) {String [] dict = {"mobile", "samsung", "sam ", "plugin", "man", "mango", "icecream", "and", "go", "I", "like", "ice ", "cream"}; for (int I = 0; I
Http://www.geeksforgeeks.org/dynamic-programming-set-32-word-break-problem/