Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,922 questions

51,855 answers

573 users

How to check if a string can split into 4 distinct substrings in PHP

1 Answer

0 votes
function canSplitInto4DistinctSubstrings($s) {
    $n = strlen($s);
    
    if ($n < 4) {
        return false; 
    }
    
    for ($i = 1; $i < $n; $i++) {
        for ($j = $i + 1; $j < $n; $j++) {
            for ($k = $j + 1; $k < $n; $k++) {
                $s1 = substr($s, 0, $i);
                $s2 = substr($s, $i, $j - $i);
                $s3 = substr($s, $j, $k - $j);
                $s4 = substr($s, $k);
                if (strlen($s1) > 0 && strlen($s2) > 0 && strlen($s3) > 0 && strlen($s4) > 0) {
                    if ($s1 !== $s2 && $s1 !== $s3 && $s1 !== $s4 &&
                        $s2 !== $s3 && $s2 !== $s4 && $s3 !== $s4) {
                        echo $s1 . " " . $s2 . " " . $s3 . " " . $s4;
                        return true;
                    }
                }
            }
        }
    }
    
    return false;
}

$str = "AlbusDumbledore";
if (canSplitInto4DistinctSubstrings($str)) {
    echo "Yes"; 
} else {
    echo "No"; 
}



/*
run:

A l b usDumbledoreYes

*/

 



answered Feb 15, 2024 by avibootz
...