function isInDict(word: string, dict: string[]): boolean {
return dict.includes(word);
}
function wordBreak(str: string, result: string, dict: string[]): void {
const strsize: number = str.length;
for (let i: number = 1; i <= strsize; i++) {
const subStr = str.substring(0, i);
if (isInDict(subStr, dict)) {
if (i === strsize) {
console.log(result + subStr);
return;
}
wordBreak(str.substring(i), result + subStr + " ", dict);
}
}
}
const str: string = "butterflyplaybasketballwithbags";
const dict: string[] = [
"butterfly", "basketball", "bagpiper", "and", "play",
"with", "butter", "fly", "basket", "ball", "bags"
];
wordBreak(str, "", dict);
/*
run:
"butter fly play basket ball with bags"
"butter fly play basketball with bags"
"butterfly play basket ball with bags"
"butterfly play basketball with bags"
*/