From 7a13a35dfd6d4563d5dab7db3547b5e8f131fc61 Mon Sep 17 00:00:00 2001 From: Anushka Verma Date: Mon, 24 Aug 2020 01:47:16 +0530 Subject: [PATCH] feat: add word break solution (backtracking) --- backtracking/word_break.cpp | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 backtracking/word_break.cpp diff --git a/backtracking/word_break.cpp b/backtracking/word_break.cpp new file mode 100644 index 000000000..b41a6aa77 --- /dev/null +++ b/backtracking/word_break.cpp @@ -0,0 +1,50 @@ +/** + * @file Word Break + * + * @details + * 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. + * + * @author [Anushka Verma](https://github.com/verma-anushka) + */ + +#include +using namespace std; + +/** + * Utility function + */ +void wordbreak(string s, vector& dict, int i, string ans) { + + string word=s.substr(0, i+1); + int ws=s.size(); + + // search for word in the dictionary + if(find(dict.begin(), dict.end(), word) != dict.end()) { + + if(i dict = { "mobile","samsung","sam","sung", + "man","mango","icecream","and", + "go","i","like","ice","cream" }; + string s = "ilikesamsung"; + wordbreak(s, dict, 0, ""); + return 0; +} +