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 Comments Improve Suggest changes Like Article Like Report 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: YesExplanation: 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 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 <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 other function 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, S var S = "abcdab"; // Store the size of S var N = S.length; // Function Call splitString(S, N); // This code contributed by shikhasingrajput </script> Output: Yes Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if a string can be split into two substrings such that one substring is a substring of the other K ketaki888 Follow Improve Article Tags : Strings Greedy Searching DSA substring +1 More Practice Tags : GreedySearchingStrings Similar Reads Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'nd substring. Examples: Input : "11235813" Output : ["1", "1", "2", "3", "5", "8", "13"] Input : "1111223" Output : ["1", "11", "12", "23"] Input : "1111213" Output : ["11", "1", "12", "13"] Input : "11121114" Out 9 min read Check if a string can be split into two substrings with equal number of vowels Given a string S, the task is to check if the string can be split into two substrings such that the number of vowels in both of them are equal. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: S = "geeks"Output: YesExplanation: Splitting the strings into substrings "ge" 15+ min read Check if a string can be split into substrings starting with N followed by N characters Given a string str, the task is to check if it can be split into substrings such that each substring starts with a numeric value followed by a number of characters represented by that numeric integer. Examples: Input: str = "4g12y6hunter" Output: Yes Explanation: Substrings "4g12y" and "6hunter" sat 5 min read Find if a given string can be represented from a substring by iterating the substring ânâ times Given a string 'str', check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. Examples: Input: str = "abcabcabc" Output: true The given string is 3 times repetition of "abc" Input: str = "abadabad" Output: true The given string is 2 times r 15+ min read Longest Substring of A that can be changed to Substring of B in at most T cost Given two strings A and B of the same length and two positive integers K and T. The task is to find the longest substring of A that can be converted to the same substring at the same position in B in less than or equal to T cost. Converting any character of A to any other character costs K units. No 13 min read Minimum splits in a binary string such that every substring is a power of 4 or 6. Given a string S composed of 0 and 1. Find the minimum splits such that the substring is a binary representation of the power of 4 or 6 with no leading zeros. Print -1 if no such partitioning is possible. Examples: Input: 100110110 Output: 3 The string can be split into a minimum of three substrings 11 min read Queries to check if string B exists as substring in string A Given two strings A, B and some queries consisting of an integer i, the task is to check whether the sub-string of A starting from index i and ending at index i + length(B) - 1 equals B or not. If equal then print Yes else print No. Note that i + length(B) will always be smaller than length(A). Exam 15+ min read Split the string into minimum parts such that each part is in the another string Given two strings A and B, the task is to split the string A into the minimum number of substrings such that each substring is in the string B. Note: If there is no way to split the string, then print -1 Examples: Input: A = "abcdab", B = "dabc" Output: 2 Explanation: The two substrings of A which i 11 min read Print all strings in the given array that occur as the substring in the given string Given an array of string arr[] and a string str, the task is to print all the strings in arr[] that occur as a substring in str. Example: Input: str ="geeksforgeeks", arr[] ={ "forg", "geek", "ek", "dog", "sfor"}Output: forggeekeksforExplanation: The strings "forg", "geek", "ek" and "sfor" occur as 5 min read Count of substrings of a string containing another given string as a substring | Set 2 Given two strings S and T of length N and M respectively, the task is to count the number of substrings of S that contains the string T in it as a substring. Examples: Input: S = âdabcâ, T = âabâOutput: 4Explanation:Substrings of S containing T as a substring are: S[0, 2] = âdabâS[1, 2] = âabâS[1, 3 8 min read Like