检查给定的字符串是否可以在Python中分为四个不同的字符串
假设我们有一个字符串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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短