首页 > 试题广场 >

由3个a,4个b和2个c构成的所有字符串中,包含子串“abc

[单选题]
由3个a,4个b和2个c构成的所有字符串中,包含子串“abc”的共有(  )个。
  • 5040
  • 4720
  • 420
  • 390
  • 30

写代码试了下还真是,就是官方的这个题解是怎么来的看不懂。

from itertools import permutations

chars = "aaabbbbcc"
ans = 0
memory = set()
for s in permutations(chars, len(chars)):
    s = "".join(s)
    if "abc" in s and s not in memory:
        ans += 1
        memory.add(s)
print(ans)
发表于 2025-10-31 16:16:56 回复(2)
发表于 2024-11-15 14:56:44 回复(1)