Python中计算一致字符串个数的程序
假设我们有一个由不同字符组成的字符串 s ,并且还有一个名为 words 的字符串数组。当字符串中的所有字符都出现在字符串 s 中时,该字符串是一致的。我们必须找到数组单词中存在的一致字符串的数量。
因此,如果输入像 s= "px", words = ["ad","xp","pppx","xpp","apxpa"],那么输出将是 3,因为只有三个字符串'p' 和 'x', ["xp","pppx","xpp"]。
示例(Python)
让我们看看以下实现以获得更好的理解 -
def solve(s, words):count = 0
for i in range(len(words)):
for j in range(len(words[i])):
if words[i][j] not in s:
break
else:
count += 1
return count
s= "px"
words = ["ad","xp","pppx","xpp","apxpa"]
print(solve(s, words))
输入
"px", ["ad","xp","pppx","xpp","apxpa"]输出结果
3
以上是 Python中计算一致字符串个数的程序 的全部内容, 来源链接: utcz.com/z/341396.html