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,923 questions

51,856 answers

573 users

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

1 Answer

0 votes
function canSplitInto4DistinctSubstrings(s) {
    if (s.length < 4)
        return false;
    
    for (let i = 2; i < s.length; i++) {
        for (let j = i + 2; j < s.length; j++) {
            for (let k = j + 2; 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 (s1.length > 0 && s2.length > 0 && s3.length > 0 && s4.length > 0) {
                    if (s1 != s2 && s1 != s3 && s1 != s4 && s2 != s3 && s2 != s4 && s3 != s4) {
                        console.log(s1, s2, s3, s4);
                        return true;
                    }
                }
            }
        }
    }
    
    return false;
}
      
let str = "aaaAlbusDumbledore";
if (canSplitInto4DistinctSubstrings(str))
    console.log("Yes");
else
    console.log("No");
 
 
 
 
/*
run:
 
aa aA lb usDumbledore
Yes
 
*/

 



answered Feb 15, 2024 by avibootz
edited May 19, 2024 by avibootz

Related questions

...