Given a string s, determine whether it can be split into four non-empty substrings such that all four substrings are pairwise distinct. Return true if such a split exists, otherwise return false.
Examples:
Input: s = "geeksforgeeks"
Output: true
Explanation: One possible split is: "geeks" , "for" , "gee" , "ks"
All four substrings are non-empty and distinct, so the output is true.
Input: s = "aaabb"
Output: false
Explanation: There is no way to split the string into four non-empty substrings such that all four substrings are distinct. Therefore, the output is false.
Use Length Observation and Check All Split Positions - O(|s|^3) Time and O(|s|) Space
The idea is to first use a length-based observation. If the string length is less than 4, it cannot be split into four non-empty substrings. If the length is at least 10, a valid split always exists, as the string can be divided into four substrings of lengths 1, 2, 3, and (n - 6), which are all distinct.
For the remaining cases, try all possible choices of three split positions to divide the string into four non-empty substrings and check whether all four substrings are pairwise distinct. If any valid partition is found, return true; otherwise, return false.
Consider the example: s = "geeksforgeeks"
- The length of the string is 13, which is greater than or equal to 10.
- First, check if the string length is less than 4. Since 13 ≥ 4, this condition is not satisfied.
- Next, check if the string length is at least 10. Since 13 ≥ 10, this condition is satisfied.
Therefore, the function directly returns true.
Since a valid split always exists when the string length is at least 10, there is no need to generate split positions or check any partitions.
#include <iostream>
using namespace std;
bool isPossible(string &s)
{
int n = s.size();
// At least 4 characters are needed to form
// four non-empty substrings.
if (n < 4)
{
return false;
}
// Any string of length 10 or more can always be split
// into four non-empty distinct substrings.
if (n >= 10)
{
return true;
}
// Try all possible ways to split the string into 4 parts.
for (int i = 1; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
for (int k = j + 1; k < n; k++)
{
string a = s.substr(0, i);
string b = s.substr(i, j - i);
string c = s.substr(j, k - j);
string d = s.substr(k);
if (a != b && a != c && a != d && b != c && b != d && c != d)
{
return true;
}
}
}
}
return false;
}
int main()
{
string s = "geeksforgeeks";
cout << (isPossible(s) ? "true" : "false") << endl;
return 0;
}
import java.util.*;
public class GFG {
public static boolean isPossible(String s)
{
int n = s.length();
// At least 4 characters are needed to form
// four non-empty substrings.
if (n < 4) {
return false;
}
// Any string of length 10 or more can always be
// split into four non-empty distinct substrings.
if (n >= 10) {
return true;
}
// Try all possible ways to split the string into 4
// parts.
for (int i = 1; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
String a = s.substring(0, i);
String b = s.substring(i, j);
String c = s.substring(j, k);
String d = s.substring(k);
if (!a.equals(b) && !a.equals(c)
&& !a.equals(d) && !b.equals(c)
&& !b.equals(d) && !c.equals(d)) {
return true;
}
}
}
}
return false;
}
public static void main(String[] args)
{
String s = "geeksforgeeks";
System.out.println(isPossible(s) ? "true"
: "false");
}
}
def isPossible(s):
n = len(s)
# At least 4 characters are needed to form
# four non-empty substrings.
if n < 4:
return False
# Any string of length 10 or more can always be split
# into four non-empty distinct substrings.
if n >= 10:
return True
# Try all possible ways to split the string into 4 parts.
for i in range(1, n):
for j in range(i + 1, n):
for k in range(j + 1, n):
a = s[:i]
b = s[i:j]
c = s[j:k]
d = s[k:]
if (a != b and a != c and a != d and
b != c and b != d and c != d):
return True
return False
if __name__ == "__main__":
s = "geeksforgeeks"
print("true" if isPossible(s) else "false")
using System;
class GFG {
static bool isPossible(string s)
{
int n = s.Length;
// At least 4 characters are needed to form
// four non-empty substrings.
if (n < 4) {
return false;
}
// Any string of length 10 or more can always be
// split into four non-empty distinct substrings.
if (n >= 10) {
return true;
}
// Try all possible ways to split the string into 4
// parts.
for (int i = 1; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
string a = s.Substring(0, i);
string b = s.Substring(i, j - i);
string c = s.Substring(j, k - j);
string d = s.Substring(k);
if (a != b && a != c && a != d && b != c
&& b != d && c != d) {
return true;
}
}
}
}
return false;
}
public static void Main(string[] args)
{
string s = "geeksforgeeks";
Console.WriteLine(isPossible(s) ? "true" : "false");
}
}
function isPossible(s)
{
let n = s.length;
// At least 4 characters are needed to form
// four non-empty substrings.
if (n < 4) {
return false;
}
// Any string of length 10 or more can always be split
// into four non-empty distinct substrings.
if (n >= 10) {
return true;
}
// Try all possible ways to split the string into 4
// parts.
for (let i = 1; i < n; i++) {
for (let j = i + 1; j < n; j++) {
for (let k = j + 1; k < n; k++) {
let a = s.substring(0, i);
let b = s.substring(i, j);
let c = s.substring(j, k);
let d = s.substring(k);
if (a !== b && a !== c && a !== d && b !== c
&& b !== d && c !== d) {
return true;
}
}
}
}
return false;
}
// Driver code
let s = "geeksforgeeks";
console.log(isPossible(s) ? "true" : "false");
Output
true