Minimum count of prefixes and suffixes of a string required to form a given string


Prefixes are the substring from the given string that starts from the zeroth index and can go up to length 1 to the size of the string. Similarly, the suffix is the substring of any length 1 to the size of the string and ends at the last index. We will be given two strings and have to create the first string by using any number of prefixes and suffixes of the second string in any way. If it is not possible to create the given string from the given methods then we will return -1.

Sample Examples

Input 1: string str1 = “tutorialspoint”  string str2 = “pointstutorial”
Output:  2

Explanation

We can use the prefix “points” and the suffix “tutorial” and by joining them we can get the first string. This only needs two substrings and that is our answer or output.

Input 2: string str1 = “randomstring” string str2 = “anotherstring” 
Output: -1

Explanation

There is no possible way by which we can get the first string from the suffixes or the prefixes of the given second string.

Approach

To solve this problem, we will use the concept of the dynamic programming that is by storing the instances that already happened.

  • First, we will create a function that will take two strings as the parameter and will return an integer.

  • In the function, first we will get the lengths of the strings and create a hashset and a temporary string to get the prefixes.

  • We will traverse over the second string and get all the prefixes and will store them in the hashset. Similarly, by traversing over the string from back we can get all the suffixes and store them in the hashset.

  • Then we will create an array to store the dynamic programming results and will store -1 at each index of the array.

  • Using nested for loops, we will break the first string into the chunks and will find whether it is possible to connect all the chunks from the hashmap.

  • We will try to reduce the number of substrings required and if not possible will return -1.

Example

#include <bits/stdc++.h>
using namespace std;
// function to find the required number of substrings
int count(string str1, string str2){
   unordered_set<string> st; // set to store the strings from the prefix and suffix 
   string temp = "";// string to store the prefixes and suffixes 
   int len1 = str1.size(); // size of the first string 
   int len2 = str2.size(); // getting size of the second 
   
   // getting prefixes of string second 
   for (int i = 0; i < len2; i++){
      temp += str2[i]; // adding the characters to temporary string
      st.insert(temp); // insert current string to set 
   }
   
   // getting all the sufixes 
   for (int i = len2-1; i >= 0; i--){
      temp = str2.substr(i); // getting suffixes		
      st.insert(temp); // adding suffixes to the set 
   }
   // Initialize memo array to store the answer
   int memo[len1+1];
   memset(memo, -1, sizeof(memo)); // initializing array 
   memo[0] = 0; 
   
   // applying the concepts of dp 
   // traversing over the main string and will try to 
   // partition string into chunks 
	for (int i = 0; i < len1; i++){
      for (int j = 1; j <= len1 - i; j++){
		   // check if the set contain current substring 
         if (st.find(str1.substr(i, j)) != st.end()){
            if (memo[i] == -1){
               continue;
            }
            if (memo[i + j] == -1){
               memo[i + j] = memo[i] + 1;
            }
            else {
               memo[i + j] = min(memo[i + j], memo[i] + 1);
            }
         }
      }
   }
   // Return the answer
   return memo[len1];
}
int main(){
   string str1 = "tutorialpoints";
   string str2 = "pointstutorial";
   // calling function to find the minimum number of substrings required
   cout <<"The minimum count of prefixes and suffixes of a string required to form given string is "<<count(str1, str2)<<endl;
   return 0;
}

Output

The minimum count of prefixes and suffixes of a string required to form given string is 2

Time and Space Complexity

The time complexity of the above code is O(N^2), as we are getting all the suffixes with that much complexity, but it could be reduced by reversing the string. Also, we are storing the strings into the hashset makes that much time complexity and nested for loop for dynamic programming.

The space complexity of the above code is O(N^2), as we are storing all the suffixes and prefixes in the hashmap.

Conclusion

In this tutorial, we have implemented a code to find the minimum number of substrings that are suffixes and prefixes of the given string to create another given string and if it is not possible, we have printed -1. We have used the concept of dynamic programming by storing the elements in the hashset and then used the nested loops with the time and space complexity of O(N^2).

Updated on: 26-Jul-2023

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements