Check if a string can be split into 3 substrings such that one of them is a substring of the other two


In this problem, we need to split the given string in such a way that the third substring can be a substring of the first two substrings.

Let’s think about the solution. The third string can be a substring of the first two string only if the first two string contains all characters of the third string. So, we need to find at least one character in the given string with a frequency of more than 3, and we can take the third substring of the single character.

Problem statement − We have given a string str containing the N lowercase alphabetic characters. We need to check whether we can split the string into three substrings, a, b, and c, such that substring c is the substring of the a and b. Print ‘yes’ or ‘no’ according to whether we can find 3 substrings.

Sample Examples

Input –  str = "tutorialsPoint"
Output – ‘Yes’

Explanation

Here, we can split the string into the ‘tu’, ‘torialsPoin’, and ‘t’. So, the third string is a substring of the first two strings.

Input –  str = "tutorials"
Output – ‘No’

Explanation

We can’t split the string into three substrings according to the given conditions, as any character doesn’t have a frequency greater than 3.

Input –  str = "hhhhhhhh"
Output – ‘Yes’

Explanation

According to the given condition, the three substrings can be ‘h’, ‘h’, and ‘hhhhhh.’

Approach 1

In this approach, we will use an array to store the frequency of each character. After that, we will check for the character with a frequency greater than or equal to 3.

Algorithm

  • Step 1 − Define the ‘freq’ array of length equal to 26.

  • Step 2 − Traverse the string to count the frequency of characters. In the for loop, increment the value of the freq[str[i] – ‘a’]. Here, str[i] – ‘a’ gives the index between 0 and 26.

  • Step 3 − Now, traverse the ‘freq’ array, and if the value at any array index is greater than ‘3’, return true.

  • Step 4 − return true when the loop terminates.

  • Step 5 − Print the ‘yes’ or ‘no’ based on the returned value from the isSUbStringPossible() function.

Example

#include <bits/stdc++.h>
using namespace std;
// function to Check if a string can be split into 3 substrings such that one of them is a substring of the other two
string isSubStringPossible(string str, int len){

   // array to store the frequency
   int freq[26] = {0};
   
   // Iterate over the string
   for (int i = 0; i < len; i++){
   
      // count the frequency of each character
      freq[str[i] - 'a']++;
   }
   
   // Traverse the frequency array
   for (int i = 0; i < 26; i++){
   
      // If the frequency of any character is greater than or equal to 3, then return "Yes."
      if (freq[i] >= 3){
         return "Yes";
      }
   }
   
   // Otherwise
   return "No";
}
int main(){
   string str = "tutorialsPoint";
   int len = str.length();
   cout << "The given string can be splited into 3 substrings such that one of them is a substring of the other two - " << isSubStringPossible(str, len);
   return 0;
}

Output

The given string can be splited into 3 substrings such that one of them is a substring of the other two - Yes

Time complexity − O(N), as we traverse the string.

Space complexity − O(1), as we use the array of the constant length.

Approach 2

In this approach, we first convert the string into an array of characters. After that, we use the count() method to count the frequency of a particular character in the array.

Algorithm

  • Step 1 − Create an array of the ‘len + 1’ size where ‘len’ is the string length.

  • Step 2 − Use the strcpy() method to copy the string into the char array.

  • Step 3 − Use the for loop to make total 26 iterations.

  • Step 4 − In the for loop, use the count() method to count the occurrence of a particular character.

  • The count() method takes a reference to the start position as a first parameter, a reference to the ending position as a second parameter, and a character as a third parameter.

    Here, we need to pass the character’s ASCII value as a parameter, which we get using the I + ‘a’.

  • Step 5 − Return true if the count() method returns greater than or equal to 3.

  • Step 6 − return false when the loop terminates.

Example

#include <bits/stdc++.h>
using namespace std;
// function to Check if a string can be split into 3 substrings such that one of them is a substring of the other two
string isSubStringPossible(string str, int len){

   //    converting str to char array.
   char char_array[len + 1];
   
   // copy string to char array
   strcpy(char_array, str.c_str());
   
   // make 26 iterations
   for (int i = 0; i < 26; i++){
   
      // Using count() to count the occurrence of each character in the array, and return 'yes' if any character occurs more than 2 times.
      if (count(char_array, char_array + len, i + 'a') >= 2)
         return "YES";
   }
   return "NO";
}
int main(){
   string str = "tutorials";
   int len = str.length();
   cout << "The given string can be splited into 3 substrings such that one of them is a substring of the other two - " << isSubStringPossible(str, len);
   return 0;
}

Output

The given string can be splited into 3 substrings such that one of them is a substring of the other two - YES

Time complexity − O(N), as the count() method iterates the char array to count characters. Also, the strcpy() method takes O(N) time.

Space complexity − O(N), as we store the string into the character array.

Conclusion

We learned two approaches to split the string into three substrings such that one can be a substring of the other two. The second approach’s code is more readable and beginner friendly, but it is more time and space expensive.

Updated on: 28-Jul-2023

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements