我有两个字符串:一个是单词,一个是字母乱序。我想看看这一串字母是否有足够的字母拼写这个单词。我想出了一个算法来实现这一点,但它不够有效,我希望我能得到一些帮助,使它更快。在
以下是我目前所掌握的情况:
s1 = 'hypochondriac'
s2 = 'yqhpwoewnqlchpijcdrxpoa'
temp = list(s1)
for X in s2:
for Y in temp:
if X == Y:
temp.remove(X)
X = '@'
if temp == []:
print('Found ', s1)
我有一个问题,一旦X匹配,我需要增加X,但我不知道怎么做,所以我把它从等式中去掉,把它变成at符号。我尝试过使用break,但是它没有达到足够远的程度来中断到s2循环。不管怎样,我很确定这种双循环的想法与有经验的人相比是非常缓慢的。有什么想法吗?在
你的代码效率不高,不,因为你在一个双循环中迭代。对于}。在
s1
中的每个字母,在最坏的情况下(没有匹配项),您将遍历所有的{请改用^{} object ;它们充当多集,在这里您既可以测试O(1)时间内是否存在字符,也可以管理剩余的计数:
您还可以将
^{pr2}$s1
转换为多集,并检查s2
的多集是否包含足够的字母来表示每个条目:后者可以使用^{} function 进一步减少,如果传递的结果是},否则返回{},否则:
False
,则返回{在所有这些中,您只需在
s1
和s2
上迭代一次(直接或生成多集)。在后者演示:
扩展@Martijn\u Pieters解决方案,您可以这样使用
Counter
:如果
key
不存在,Counter[key]
将默认返回0。在换个方向做。删除
s2
中的字符:相关问题 更多 >
编程相关推荐