微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

字符串中的单词等于另一个字符串中单词的排列

如何解决字符串中的单词等于另一个字符串中单词的排列

我想知道一个字符串是否至少等于另一个字符串的排列之一。 例如,下面两个比较的输出必须是true

print('united states of America' == 'America united states of')
print('united states of America' == 'states of united America')

我使用了 itertools.permutations 方法来查找排列,但是 我想要的是单词而不是句子或字符串中的字母。

所以不是 'abcd' == 'bcad' 等于 True,而是 'a b c d' == 'b c a d' 应该是 True

解决方法

您不需要itertools.permutations。相反,比较重新排序的字符串,所有单词都已排序:

def reorder_str(s):
    return " ".join(sorted(s.split()))

print(reorder_str('united states of America') == reorder_str('America united states of'))
# True

print(reorder_str('united states of America') == reorder_str('america united states of'))
# False
,

应该这样做:

import itertools
def f(s1,s2):
   perms = itertools.permutations(s1.split(" "))
   return tuple(s2.split(" ")) in perms

>>> f('united states of America','America united states of')
True
>>> f('united states of America','states of united America')
True
>>> f("a b c d","b a c d")
True
,

我推荐的解决方案是找到每个字符串中每个单词的频率并进行比较。

实际实现有点棘手,我们首先找到第一个字符串中每个单词的频率,然后对于第二个字符串中的每个单词我们将频率减1,如果频率小于0,我们知道第二个字符串比第一个字符串有更多次这个词,我们可以跳过其余的词。

def compare(s1,s2):
    count = {}  # this is the frequencies dict
    for word in s1.split():
        count[word] = count.get(word,0) + 1  # increment the frequency of the word
    for word in s2.split():
        count[word] = count.get(word,0) - 1  # decrement the frequency of the word
        if count[word] < 0:
            # s2 has more of this word than s1
            return False
    return True

此算法比排序或 itertools.permutations 更快,但占用更多空间。该算法的时间复杂度为 O(n),其中 n 是单词数,而 O(nlog n) 用于排序,O(n!) 用于 itertools.permutations

,

只需检查词频

from collections import Counter

c = Counter('united states of America')
c1 = Counter('America united states of')
c2 = Counter('states of united America'

print(c == c1 == c2)
# True
,

与排列相比,在计算上更有效的方法是拆分和排序字符串并比较排序的列表。生成排列的一个挑战是,一旦你开始只得到几个词,例如10,那么你就有10!生成的排列数 (3628800),您可以将其与字符串进行比较,使其非常低效且非常快。

string_1 = "states of united America"
string_2 = "united states of America"

def check_if_perm(s1,s2):
    return sorted(s1.split()) == sorted(s2.split())

输出:

True
,

如果 itertools.permutations 不是必须的,我会使用集合:

>>> set('united states of America'.split()) == set('America united states of'.split())
True
>>> set('united states of America'.split()) == set('America united states off'.split())
False

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?