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

• Last Updated : 13 May, 2021

Given a string S of length N, the task is to check if a string can be split into two substrings, say A and B such that B is a substring of A. If not possible, print No. Otherwise, print Yes.

Examples :

Input: S = “abcdab”
Output: Yes
Explanation: Considering the two splits to be A=”abcd” and B=”ab”, B is a substring of A.

Input: S = “abcd”
Output: No

Naive Approach: The simplest approach to solve the problem is to split the string S at every possible index, and check if the right substring is a substring of the left substring. If any split satisfies the condition, print “Yes“. Otherwise, print “No“.

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to check if the last character of the string S is present in the remaining string or not. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program to implement``// the above approach``#include ``using` `namespace` `std;` `// Function to check if a string can be``// divided into two substrings such that``// one substring is substring of the other``void` `splitString(string S, ``int` `N)``{``    ``// Store the last character of S``    ``char` `c = S[N - 1];` `    ``int` `f = 0;` `    ``// Traverse the characters at indices [0, N-2]``    ``for` `(``int` `i = 0; i < N - 1; i++) {` `        ``// Check if the current character is``        ``// equal to the last character``        ``if` `(S[i] == c) {` `            ``// If true, set f = 1``            ``f = 1;` `            ``// Break out of the loop``            ``break``;``        ``}``    ``}` `    ``if` `(f)``        ``cout << ``"Yes"``;``    ``else``        ``cout << ``"No"``;``}` `// Driver Code``int` `main()``{``    ``// Given string, S``    ``string S = ``"abcdab"``;` `    ``// Store the size of S``    ``int` `N = S.size();` `    ``// Function Call``    ``splitString(S, N);` `    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``import` `java.util.*;``class` `GFG{` `// Function to check if a String can be``// divided into two subStrings such that``// one subString is subString of the other``static` `void` `splitString(String S, ``int` `N)``{``  ` `    ``// Store the last character of S``    ``char` `c = S.charAt(N - ``1``);``    ``int` `f = ``0``;` `    ``// Traverse the characters at indices [0, N-2]``    ``for` `(``int` `i = ``0``; i < N - ``1``; i++)``    ``{` `        ``// Check if the current character is``        ``// equal to the last character``        ``if` `(S.charAt(i) == c)``        ``{` `            ``// If true, set f = 1``            ``f = ``1``;` `            ``// Break out of the loop``            ``break``;``        ``}``    ``}` `    ``if` `(f > ``0``)``        ``System.out.print(``"Yes"``);``    ``else``        ``System.out.print(``"No"``);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``  ` `    ``// Given String, S``    ``String S = ``"abcdab"``;` `    ``// Store the size of S``    ``int` `N = S.length();` `    ``// Function Call``    ``splitString(S, N);``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to check if a can be``# divided into two substrings such that``# one subis subof the other``def` `splitString(S, N):``    ` `    ``# Store the last character of S``    ``c ``=` `S[N ``-` `1``]` `    ``f ``=` `0` `    ``# Traverse the characters at indices [0, N-2]``    ``for` `i ``in` `range``(N ``-` `1``):``        ` `        ``# Check if the current character is``        ``# equal to the last character``        ``if` `(S[i] ``=``=` `c):``            ` `            ``# If true, set f = 1``            ``f ``=` `1``            ` `            ``# Break out of the loop``            ``break` `    ``if` `(f):``        ``print``(``"Yes"``)``    ``else``:``        ``print``(``"No"``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given string, S``    ``S ``=` `"abcdab"` `    ``# Store the size of S``    ``N ``=` `len``(S)` `    ``# Function Call``    ``splitString(S, N)``    ` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program to implement``// the above approach ``using` `System;``class` `GFG{`` ` `// Function to check if a string can be``// divided into two substrings such that``// one substring is substring of the other``static` `void` `splitString(``string` `S, ``int` `N)``{``    ``// Store the last character of S``    ``char` `c = S[N - 1];``    ``int` `f = 0;`` ` `    ``// Traverse the characters at indices [0, N-2]``    ``for` `(``int` `i = 0; i < N - 1; i++)``    ``{`` ` `        ``// Check if the current character is``        ``// equal to the last character``        ``if` `(S[i] == c)``        ``{`` ` `            ``// If true, set f = 1``            ``f = 1;`` ` `            ``// Break out of the loop``            ``break``;``        ``}``    ``}`` ` `    ``if` `(f != 0)``        ``Console.Write(``"Yes"``);``    ``else``        ``Console.Write(``"No"``);``}`` ` `// Driver code``public` `static` `void` `Main()``{``    ``// Given string, S``    ``string` `S = ``"abcdab"``;`` ` `    ``// Store the size of S``    ``int` `N = S.Length;`` ` `    ``// Function Call``    ``splitString(S, N);``}``}` `// This code is contributed by susmitakundugoaldanga`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up