Minimum Word Break Problem in C++


We are given an array string of words of any given size and the task is to break the words in all possible ways such that after the break the string formed should be a valid string and we have to calculate all such minimum word break as per the problem.

Let us see various input output scenarios for this -

In − string word[] = {"Hello", "Hell", "tell", "well", "bell", "ball", "all" }

Out − Minimum Word Break is: 1

Explanation − we are given with multiple words. Now we will pass the concatenation of two strings i.e. Hell and all and will break the concatenated word. So, Hellall can be divided as “Hell all” that is the first break and both the words are valid. Now, we will break it further i.e. “He ll aa” which is not forming any valid string. So the output is 1.

In − string word[] = {"Hello", "Hell", "tell", "well", "bell", "ball", "all" }

Out − Minimum Word Break is: 1

Explanation − we are given with multiple words. Now we will pass the concatenation of two strings i.e. Hell and well and will break the concatenated word. So, Hellwell can be divided as “Hell well” that is the first break and both the words are valid. Now, we will break it further i.e. “He ll well” which is not forming any valid string. So the output is 1.

Approach used in the below program is as follows

  • Input the string array of words and calculate the size using the size() function.

  • Declare a variable as min_val and set it to INT_MAX as the maximum possible value.

  • Create an object of type structure as root and set it to the call to the function Return_Node()

  • Start loop FOR from i to 0 till the size of an array. Inside the loop, call the function Insert_Node() to insert the node in a tree.

  • Call the Minimum_Break() to calculate the minimum word break possible and print the final result.

  • Declare a structure to construct the tree of nodes with words as data.

    • Create an array of pointers as ptr[total_Alpha] and a boolean variable as check.

  • Inside the Return_Node(void)

    • Create a pointer of type structure as ptr_1. Set ptr_1->check as false

    • Start Loop FOR from i to 0 till i less than total_Alpha. Inside the loop, set ptr_1- >ptr[i] to NULL

    • Return ptr_1

  • Inside the function Insert_Node(struct node* root, string val)

    • Create a pointer as ptr_1 and set it to root.

    • Start loop FOR from i to 0 till the val.length(). Inside the loop, set key as val[i] - ‘a’ and check IF ptr_1->ptr[key] is NULL then set ptr_1->ptr[key] to the call of function Return_Node().

    • Set ptr_1 to ptr_1->ptr[key] and ptr_1->check to true

  • Inside the function Minimum_Break(struct node* root, string val, int first, int* temp, int a = 0)

    • Create a pointer as ptr_1 and set it to root.

    • Check IF first = val.length() then set *temp to call to the inbuilt function of C++ which is min(*temp, a - 1) and return.

    • Start loop FOR from i to first till i less than length of a val. Inside the loop, set address as val[i] - 'a' and check IF ptr_1->ptr[address] is null then return.

    • Check IF ptr_1->ptr[address]->check is true then call Minimum_Break(root, val, i + 1, temp, a + 1)

    • Set ptr_1 to ptr_1->ptr[address].

Example

#include <bits/stdc++.h>
using namespace std;
#define total_Alpha 26
//create a tree of nodes of words
struct node{
   struct node* ptr[total_Alpha];
   bool check;
};
//Return tree with all nodes
struct node* Return_Node(void){
   struct node* ptr_1 = new node;
   ptr_1->check = false;
   for (int i = 0; i < total_Alpha; i++){
      ptr_1->ptr[i] = NULL;
   }
   return ptr_1;
}
//insert values to the nodes in a tree
void Insert_Node(struct node* root, string val){
   struct node* ptr_1 = root;

   for(int i = 0; i < val.length(); i++){
      int key = val[i] - 'a';
      if(!ptr_1->ptr[key]){
         ptr_1->ptr[key] = Return_Node();
      }
      ptr_1 = ptr_1->ptr[key];
   }
   ptr_1->check = true;
}
//calculate the minimum word break
void Minimum_Break(struct node* root, string val, int first, int* temp, int a = 0){
   struct node* ptr_1 = root;
   if(first == val.length()){
      *temp = min(*temp, a - 1);
      return;
   }
   for(int i = first; i < val.length(); i++){
      int address = val[i] - 'a';
      if(!ptr_1->ptr[address]){
         return;
      }
      if(ptr_1->ptr[address]->check){
         Minimum_Break(root, val, i + 1, temp, a + 1);
      }
      ptr_1 = ptr_1->ptr[address];
  }
}
int main(){
   string word[] = {"Hello", "Hell", "tell", "well", "bell", "ball", "all" };
   int size = sizeof(word) / sizeof(word[0]);
   int min_val = INT_MAX;
   struct node* root = Return_Node();
   for (int i = 0; i < size; i++){
      Insert_Node(root, word[i]);
   }
   Minimum_Break(root, "Hellall", 0, &min_val, 0);
   cout<<"Minimum Word Break is: "<< min_val;
   return 0;
}

Output

If we run the above code it will generate the following Output

Minimum Word Break is: 1

Updated on: 22-Oct-2021

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements