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

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:
- Store the last character of S in c.
- Check if c is present in the substring S[0, N-2].
- If found to be true, print “YES”. otherwise print “NO”.
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to check if a string can be// divided into two substrings such that// one substring is substring of the othervoid 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 Codeint 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 approachimport java.util.*;class GFG{// Function to check if a String can be// divided into two subStrings such that// one subString is subString of the otherstatic 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 Codepublic 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 otherdef 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 Codeif __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 otherstatic 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 codepublic 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
<script>// JavaScript program to implement// the above approach// Function to check if a String can be// divided into two subStrings such that// one subString is subString of the otherfunction splitString(S , N){ // Store the last character of S var c = S.charAt(N - 1); var f = 0; // Traverse the characters at indices [0, N-2] for (var 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) document.write("Yes"); else document.write("No");}// Driver Code//Given String, Svar S = "abcdab";// Store the size of Svar N = S.length;// Function CallsplitString(S, N);// This code contributed by shikhasingrajput </script> |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



