Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S consisting of N lowercase alphabets, the task is to check if it is possible to split the string S into three non-empty substrings such that Y is the substring of the strings X and Z. If it is possible to split the string S then, print “Yes”. Otherwise, print “No”.

Examples:

Input: S = “geekseekforgeeks”
Output: Yes
Explanation:
The given string S = “geeksforgeeks” can be splitted as “geeks”, “eek”, and Z = “forgeeks”.
The string “eeks” is a substring of both the strings “geeks” and “forgeeks”.

Input: S = “naturalxws”
Output: No

Approach: The given problem can be solved by storing the frequency of unique characters and observe the fact that if there exists any character having frequency at least 3, then the string can be split into 3 substrings satisfying the given conditions. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if string S contains
// any character with frequency >= 3 or not
string freqCheck(string S, int N)
{
    // Stores frequency of characters
    int hash[26] = { 0 };
 
    // Iterate over the string
    for (int i = 0; i < N; i++) {
 
        // Update the frequency
        // of current character
        hash[S[i] - 'a']++;
    }
 
    // Iterate over the hash array
    for (int i = 0; i < 26; i++) {
 
        // If any character has
        // frequency >= 3
        if (hash[i] > 2) {
            return "Yes";
        }
    }
 
    // Otherwise
    return "No";
}
 
// Driver Code
int main()
{
    string S = "geekseekforgeeks";
    int N = S.length();
    cout << freqCheck(S, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to check if string S contains
// any character with frequency >= 3 or not
static String freqCheck(String S, int N)
{
     
    // Stores frequency of characters
    int hash[] = new int[26];
 
    // Iterate over the string
    for(int i = 0; i < N; i++)
    {
         
        // Update the frequency
        // of current character
        hash[S.charAt(i) - 'a']++;
    }
 
    // Iterate over the hash array
    for(int i = 0; i < 26; i++)
    {
         
        // If any character has
        // frequency >= 3
        if (hash[i] > 2)
        {
            return "Yes";
        }
    }
 
    // Otherwise
    return "No";
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "geekseekforgeeks";
    int N = S.length();
     
    System.out.println(freqCheck(S, N));
}
}
 
// This code is contributed by Kingash


Python3




# Python3 program for the above approach
 
# Function to check if string S contains
# any character with frequency >= 3 or not
def freqCheck(S, N):
 
    # Stores frequency of characters
    hash = [0] * 26
 
    # Iterate over the string
    for i in range(N):
 
        # Update the frequency
        # of current character
        hash[ord(S[i]) - ord('a')] += 1
 
    # Iterate over the hash array
    for i in range(26):
 
        # If any character has
        # frequency >= 3
        if (hash[i] > 2):
            return "Yes"
 
    # Otherwise
    return "No"
 
# Driver Code
if __name__ == "__main__":
 
    S = "geekseekforgeeks"
    N = len(S)
     
    print(freqCheck(S, N))
     
# This code is contributed by ukasp


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if string S contains
// any character with frequency >= 3 or not
static string freqCheck(string S, int N)
{
     
    // Stores frequency of characters
    int[] hash = new int[26];
 
    // Iterate over the string
    for(int i = 0; i < N; i++)
    {
         
        // Update the frequency
        // of current character
        hash[S[i] - 'a']++;
    }
 
    // Iterate over the hash array
    for(int i = 0; i < 26; i++)
    {
         
        // If any character has
        // frequency >= 3
        if (hash[i] > 2)
        {
            return "Yes";
        }
    }
 
    // Otherwise
    return "No";
}
 
// Driver code
static public void Main()
{
    string S = "geekseekforgeeks";
    int N = S.Length;
     
    Console.WriteLine(freqCheck(S, N));
}
}
 
// This code is contributed by offbeat


Javascript




<script>
    // Javascript program for the above approach
 
// Function to check if string S contains
// any character with frequency >= 3 or not
function freqCheck(S, N){
 
    // Stores frequency of characters
    let hash = new Array(26).fill(0)
 
    // Iterate over the string
    for(let i = 0; i < N; i++){
 
        // Update the frequency
        // of current character
        hash[S.charCodeAt(i) - 'a'.charCodeAt(0)] += 1
    }
    // Iterate over the hash array
    for(let i = 0; i < 26; i++){
 
        // If any character has
        // frequency >= 3
        if (hash[i] > 2){
            return "Yes"
        }
    }
 
    // Otherwise
    return "No"
}
// Driver Code
 
    let S = "geekseekforgeeks"
    let N = S.length
     
    document.write(freqCheck(S, N))
     
// This code is contributed by gfgking
</script>


Output: 

Yes

 

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

 



Last Updated : 03 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads