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

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 ` `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

 ``

Output:

`Yes`

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

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next