Given a string, the task is to check if we can split it into 4 strings such that each string is non-empty and different from the other.
Examples:
Input : str[] = "geeksforgeeks"
Output : Yes
"geeks", "for", "gee", "ks" are four
distinct strings that can form from
given string.
Input : str[] = "aaabb"
Output : No
Observe if the length of the string is greater than or equal to 10, then every time can split into four parts. Suppose, the length is of 10, then string of length 1, 2, 3, 4 can be made.
For string having length less than 10, we can use brute force i.e iterate through all possible ways of splitting the string and check each one.
If length is more than 10
return true
Else (If length is less than 10)
Use Brute Force method to check
if we can break it in four distinct
strings.
Below is the implementation of the above idea.
C++
#include<bits/stdc++.h>
using namespace std;
bool check(string s)
{
if (s.size() >= 10)
return true ;
for ( int i =1; i < s.size(); i++)
{
for ( int j = i + 1; j < s.size(); j++)
{
for ( int k = j + 1; k < s.size(); k++)
{
string s1 = s.substr(0, i);
string s2 = s.substr(i, j - i);
string s3 = s.substr(j, k - j);
string s4 = s.substr(k, s.size() - k);
if (s1 != s2 && s1 != s3 && s1 != s4 &&
s2 != s3 && s2 != s4 && s3 != s4)
return true ;
}
}
}
return false ;
}
int main()
{
string str = "aaabb" ;
(check(str))? (cout << "Yes" << endl):
(cout << "No" << endl);
return 0;
}
|
Java
class GFG
{
public static boolean strcheck(String s1, String s2)
{
if (s1 != s2)
return false ;
return true ;
}
public static boolean check(String s)
{
if (s.length() >= 10 )
return true ;
for ( int i = 1 ; i < s.length(); i++)
{
for ( int j = i + 1 ; j < s.length(); j++)
{
for ( int k = j + 1 ; k < s.length(); k++)
{
String s1 = "" , s2 = "" , s3 = "" , s4 = "" ;
try
{
s1 = s.substring( 0 , i);
s2 = s.substring(i, j - i);
s3 = s.substring(j, k - j);
s4 = s.substring(k, s.length() - k);
}
catch (StringIndexOutOfBoundsException e) {
}
if (strcheck(s1, s2) && strcheck(s1, s3) &&
strcheck(s1, s4) && strcheck(s2, s3) &&
strcheck(s2, s4) && strcheck(s3, s4))
return true ;
}
}
}
return false ;
}
public static void main(String[] args)
{
String str = "aaabb" ;
if (check(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check(s):
if ( len (s) > = 10 ):
return True
for i in range ( 1 , len (s)):
for j in range (i + 1 , len (s)):
for k in range (j + 1 , len (s)):
s1 = s[ 0 :i]
s2 = s[i:j - i]
s3 = s[j: k - j]
s4 = s[k: len (s) - k]
if (s1 ! = s2 and s1 ! = s3 and s1 ! = s4 and
s2 ! = s3 and s2 ! = s4 and s3 ! = s4):
return True
return False
if __name__ = = '__main__' :
str = "aaabb"
print ( "Yes" ) if (check( str )) else print ( "NO" )
|
C#
using System;
class GFG
{
public static Boolean strcheck(String s1,
String s2)
{
if (s1.CompareTo(s2) != 0)
return false ;
return true ;
}
public static Boolean check(String s)
{
if (s.Length >= 10)
return true ;
for ( int i = 1; i < s.Length; i++)
{
for ( int j = i + 1; j < s.Length; j++)
{
for ( int k = j + 1; k < s.Length; k++)
{
String s1 = "" , s2 = "" ,
s3 = "" , s4 = "" ;
try
{
s1 = s.Substring(0, i);
s2 = s.Substring(i, j - i);
s3 = s.Substring(j, k - j);
s4 = s.Substring(k, s.Length - k);
}
catch (Exception e) {
}
if (strcheck(s1, s2) && strcheck(s1, s3) &&
strcheck(s1, s4) && strcheck(s2, s3) &&
strcheck(s2, s4) && strcheck(s3, s4))
return true ;
}
}
}
return false ;
}
public static void Main(String[] args)
{
String str = "aaabb" ;
if (check(str))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function strcheck(s1, s2)
{
if (s1.localeCompare(s2) != 0)
return false ;
return true ;
}
function check(s)
{
if (s.length >= 10)
return true ;
for (let i = 1; i < s.length; i++)
{
for (let j = i + 1; j < s.length; j++)
{
for (let k = j + 1; k < s.length; k++)
{
let s1 = "" , s2 = "" , s3 = "" , s4 = "" ;
s1 = s.substring(0, i);
s2 = s.substring(i, i + j - i);
s3 = s.substring(j, j + k - j);
s4 = s.substring(k, k + s.length - k);
if (strcheck(s1, s2) && strcheck(s1, s3) &&
strcheck(s1, s4) && strcheck(s2, s3) &&
strcheck(s2, s4) && strcheck(s3, s4))
return true ;
}
}
}
return false ;
}
let str = "aaabb" ;
if (check(str))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity : O(n3)
Auxiliary Space : O(n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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!
Last Updated :
22 Feb, 2023
Like Article
Save Article