假设我们有一个字符串s,我们必须检查是否可以将它分成四个子字符串,以使每个子字符串都是非空且唯一的。
因此,如果输入类似于s =“ helloworld”,则输出将为True,因为可能的一组子字符串之一为[“ hel”,“ lo”,“ wor”,“ ld”]
让我们看下面的实现以更好地理解-
def solve(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)): sub1 = s[0:i] sub2 = s[i:j - i] sub3 = s[j: k - j] sub4 = s[k: len(s) - k] if sub1 != sub2 and sub1 != sub3 and sub1 != sub4 and sub2 != sub3 and sub2 != sub4 and sub3 != sub4: return True return False s = "helloworld" print (solve(s))
"helloworld"输出结果
True