【问题标题】:array of substrings in array of strings字符串数组中的子字符串数组
【发布时间】:2019-06-16 09:29:30
【问题描述】:

我有两个字符串数组。一个数组中的字符串可能是另一个数组中字符串的子集。我需要找出一个数组中的所有字符串是另一个数组中字符串的子字符串

例子:

arr1 = ["firestorm", "peanut", "earthworm"]
arr2 = ["fire", "tree", "worm", "rest"]

结果:

res = ["fire","worm", "rest"]

下面提到了我的解决方案。但这需要很多时间。我必须处理数千个单词。

解决方案:

res =[]
arr1.each do |word1|
  arr2.each do |word2|
   if word1.include? word2
     res << word2
   end
  end
end

请建议我更快的方法来做到这一点

【问题讨论】:

  • 如果您有解决方案,请将其包含在您的问题中。我们试图重新发明您已经完成的工作毫无意义,我们可以在您现有的工作的基础上再接再厉。
  • 假设arr1 = ['ab']arr2 = ['b', 'a']。期望的返回值是多少,为什么?
  • ruby 显然不擅长这样的事情。你应该考虑redis中的全文搜索功能。 github.com/vruizext/redisearch-rb
  • “子集”一词意味着您提到arr1arr2 的子集。你真的是想说“子字符串”吗?
  • 我错过了什么吗?为什么投反对票?

标签: arrays ruby string substring


【解决方案1】:

很遗憾,我们不知道您的解决方案。

但是 Array 比 String 占用更多的内存空间。所以你可以转换它。

arr1 = ["firestorm", "peanut", "earthworm"]
arr2 = ["fire", "tree", "worm", "rest"]

arr1 = arr1.join(',')

然后

res = arr2.select { |word| arr1.include?(word) } #=> ["fire", "worm", "rest"]

res = arr2.select { |word| arr1.match?(word) } #=> ["fire", "worm", "rest"]

res = arr2.select { |word| arr1.match(word) } #=> ["fire", "worm", "rest"]

【讨论】:

  • 为什么需要第二种方式的字符串插值?
【解决方案2】:

据我所知,由于术语重叠,您需要暴力破解:

def matched(find, list)
  list.flat_map { |e| find.flat_map { |f| e.scan(f) } }.uniq
end

在实践中:

matched(%w[ fire tree worm rest ], %w[ firestorm peanut earthworm ])
# => ["fire", "rest", "worm"]

这里%w 被用作表达列表的更快捷方式。

这是使用scanflat_map 的近似值:

def matched(find, list)
  rx = Regexp.union(find)

  list.flat_map { |e| e.scan(rx) }.uniq
end

在使用Rexexp.union 的地方,您可以创建一个与单个测试相比运行速度相当快的正则表达式。

不准确的地方:

matched(%w[ fire tree worm rest ], %w[ firestorm peanut earthworm ])
# => ["fire", "worm"]

【讨论】:

    猜你喜欢
    • 2015-10-22
    • 2018-10-11
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2011-07-04
    • 2015-03-27
    相关资源
    最近更新 更多