Check if given string can be split into two substrings X and Y

Last Updated :

Given a string str. The task is to check whether this string can be splitted into two strings X and Y such that

  • Y is a substring of X
  • X + Y = str  (Here, X+Y means X and Y are concatenated)

Examples :

Input: pleaselea
Output: Yes
str = "please" + "lea", Here "lea" is a substring of "please".

Input: woohoo
Output: Yes
str = "wooh" + "oo", Here "oo" is a substring of "wooh".

Input: split
Output: No

Approaches :

Naive approach:
Simplest approach for this problem is that

  1. Run a loop for different possibilities of string X
  2. Remaining string will be string Y
  3. Check if Y is a substring of X

Time Complexity of this approach will be O(n2 ).

Optimized approach:
Here observation is that, Y is required to be a substring of X, that means each character of Y will occur in string X in the same manner. And, Y is always a suffix of given string.

We can optimize our solution by limiting length of Y as 1. We already know that Y is a suffix and will occur in X, so last character of given string must occur in X for the given condition to be fulfilled.

Now, we will take only last character of str as string Y and check if it occurs in string X (first n-1 characters).

Below is the implementation of the above approach:

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;

// function to check if given string 
// can be splitted in X and Y
bool splitString (string str)
{
  int n = str.length();
 
  // string Y
  char Y = str[n - 1];
  
  // find Y in remaining string
  for (int i=0; i<n-1; i++)
  {
    if (str[i] == Y)
      return true;
  }
  	
  return false;
}

// Driver Code
int main()
{
  // Given string
  string str = "pleaselea";
  	
  if (splitString(str))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

Output:

Yes

Time Complexity: O(n)
Space Complexity: O(n)

Comment
Next Article
Mithlesh Upadhyay Published 27 Sep, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • Find Words containing ‘a’ in given String

    Given a statement(string), the task is to check if that string contains any word which contains ‘a’ in it then print that word otherwise print no any word…

    2 min read
  • Find the Nth Triacontakaihenagonal Number

    Given a number N, the task is to find Nth triacontakaihenagonal number. A triacontakaihenagonal number is class of figurate number. It has 31 – sided polygon called triacontakaihenagon.…

    1 min read
  • Check if a Number is a Tetracontagon Number

    Given an integer N, the task is to check if it is a Tetracontagon number or not. Tetracontagon number is class of figurate number. It has 40 –…

    2 min read
  • Find max rational number in given Array

    Given the Numerator and Denominator of N Rational Numbers, the task is to find the maximum Rational Number from the array. Examples: Input: N = 2 num[] =…

    2 min read
  • Check if a Number is Pentacontagon

    Given an integer N, the task is to check if it is a pentacontagon number or not. Pentacontagon number is class of figurate number. It has 50 –…

    2 min read
  • Check if a Number is Triacontakaidigon

    Given an integer N, the task is to check if it is a Triacontakaidigon number or not. Triacontakaidigon number is class of figurate number. It has 32 –…

    2 min read