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

1 Answer

0 votes
def canSplitInto4DistinctSubstrings(s):
    size = len(s)
    
    if size < 4:
        return False

    for i in range(1, size):
        for j in range(i + 1, size):
            for k in range(j + 1, size):
                s1 = s[:i]
                s2 = s[i:j]
                s3 = s[j:k]
                s4 = s[k:]
                if (len(s1) > 0 and len(s2) > 0 and len(s3) > 0 and len(s4) > 0): 
                    if (s1 != s2 and s1 != s3 and s1 != s4 and s2 != s3 and s2 != s4 and s3 != s4):
                        print(s1, s2, s3, s4)
                        return True

    return False

s = "AlbusDumbledore"
if canSplitInto4DistinctSubstrings(s):
    print("Yes")
else:
    print("No")
    
    
    
''' 
run:

A l b usDumbledore
Yes

'''

 



answered Feb 15, 2024 by avibootz

Related questions

...