Check if 2 * K + 1 non-empty strings exists whose concatenation forms the given string


In this problem, we have given a string, and we need to divide the string into k + 1 substrings such that the concatenation of k + 1 substrings with their reverse can give us the original string.

Observation can solve the problem. If the string's first and last k characters are the same, we can say it is possible to create a k + 1 string according to the given condition.

Problem statement – We have given a string of length N containing the lowercase alphabetical characters and positive integer K. We need to find whether we can find total k + 1 strings such that concatenation of A1, A2, A3, …., Ak, A(k + 1) strings and reverse of them which is AK, A(k-1), …, A3, A2, A1 is original string s.

Sample examples

Input – str = "tutorialsotut"; k = 4

Output – Yes

Explanation– We can create a total of 5 (4 + 1) strings which are ‘t’, ‘u’, ‘t’, ‘o’, and ‘rials’, and when we concat them according to the given condition, we can get the original string.

Input – str = "ststs", k = 1

Output – Yes

Explanation– We can create ‘s’ and ‘tst’ total 2 strings.

Input– str = “spres”, k = 3

Output– No

Explanation– We can’t create a total of 4 strings so that it can follow the condition given in the problem statement.

Approach 1

In this approach, we will use the for loop to compare the first k characters of the string with the last k characters. If the first K characters are equal to the last k characters of the string, we can say that it is possible to create K + 1 strings, so when we merge them in actual and reverse order, we can get the original string.

Algorithm

  • Store the length of the string in the ‘len’ variable.

  • If 2*k + 1 is greater than the string length, return false as it is not possible to get the original string by merging k + 1 strings in actual and reverse order.

  • Traverse the first and last k characters using the for loop.

  • In th loop, compare the str[i] and str[len - i - 1] characters. If they are not the same, return false.

  • Return true if for loop completes all iterations without executing the return statement.

Example

#include <bits/stdc++.h>
using namespace std;
// function to check if we can make k+1 substrings to get the original string by concatenating strings according to the given conditions.
bool findStrings(string str, int k) {
   // get the length of the string
   int len = str.size();
   // If the length of the string is less than 2*k+1, return false
   if (2 * k + 1 > len) {
      return false;
   }
   // traverse the string to check whether the first k characters are equal to the reverse of the last k characters or not
   for (int i = 0; i < k; i++) {
      if (str[i] != str[len - i - 1]) {
          return false;
      }
   }
   // Return true if the first k characters are equal to the last k characters
   return true;
}
int main() {
   string str = "tutorialsotut";
   int K = 4;
   if (findStrings(str, K)) {
      cout << "Yes, we can make k+1 substrings according to the given conditions.";
   } else {
      cout << "No, we can't make k+1 substrings according to the given conditions.";
   }
   return 0;
}

Output

Yes, we can make k+1 substrings according to the given conditions.

Time complexity – O(K), as we traverse the first K characters of the string.

Space complexity – O(1), as we use constant space.

Approach 2

This approach uses the substr() method to get the substrings from the original string. Also, we will use the reverse() method to reverse the substring.

Here, we will compare the first k characters and the reverse of the last k characters. If they are equal, we can say it is possible to create k + 1 substrings so that after merging, we can get the actual string.

Algorithm

  • If the length of the given string is greater than 2*k + 1, return false.

  • Use the substr() method and get the substring from the [0, k] index, and store it into the ‘first’ variable.

  • Again, use the substr() method, get the substring from the [len - k, len] index, and store it into the ‘last’ variable.

  • Use the reverse() method to reverse the last string.

  • return the boolean value of first == last.

Example

#include <bits/stdc++.h>
using namespace std;
// function to check if we can make k+1 substrings so that we can get the original string by concatenating strings according to the given conditions.
bool findStrings(string str, int k) {
   // get the length of the string
   int len = str.size();
   // If the length of the string is less than 2*k+1, return false
   if (2 * k + 1 > len) {
      return false;
   }
   // Get the first k characters
   string first = str.substr(0, k);
   // Get the last k characters
   string last = str.substr(len - k, k);
   // Reverse the string
   reverse(last.begin(), last.end());
   // Return true if both the strings are equal. Otherwise, return false
   return (first == last);
}
int main() {
   string str = "ststs";
   int K = 1;
   if (findStrings(str, K)) {
      cout << "Yes, we can make k+1 substrings according to the given conditions.";
   } else {
      cout << "No, we can't make k+1 substrings according to the given conditions.";
   }
   return 0;
}

Output

Yes, we can make k+1 substrings according to the given conditions.

Time complexity – O(K), as we get the substring.

Space complexity – O(K), as we store substrings of length K in the ‘first’ and ‘last’ variables.

We learned two different approaches to solving the problem. The first approach is more optimized than the second approach as it has lower space complexity and the same time complexity.

Updated on: 18-Aug-2023

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements