包含的集合数量的简明解决方案

发布于 2024-10-11 14:59:53 字数 686 浏览 4 评论 0原文

假设我有一个简短的字符串列表,其中可以包含重复项:<“A”,“A”,“B”,“B”,“C”,“C”,“D”,“E”,“ F”>

然后假设我有一些其他字符串列表,它们可能是也可能不是原始列表的子集。我需要知道:

  1. 第二组是否“覆盖”第一组(例如,第一组中的每个项目是否也包含在第二组中)?
  2. 如果 1 为真,则重新创建第一个集合需要第二个集合的多少个实例?

所以,在这种情况下,如果我的第二组是列表:<“A”,“B”,“C”,“D”,“E”,“F”>,我会得到 TRUE 和 2。

如果这是列表:<“A”,“B”,“C”>,我会得到FALSE。

如果我的第一个是<“A”,“A”,“A”,“A”,“B”,“B”,“B”,“C”,“C”>:

  • 第二个是<“A” ”、“B”、“C”>:返回 TRUE 和 4。
  • 第二个是 <“A”、“A”、“B”、“C”>:返回 TRUE 和 3。

我知道这可以是使用嵌套循环可以在 N x M 时间内轻松完成。但我正在寻找一个简洁和/或优化的(最好是基于 Linq 的)解决方案。我使用了 Linq.Except 但问题是它只返回不同的元素,因此在比较包含重复项的字符串列表时毫无用处。

大家有什么独特的想法吗?

Let's say I have a short list of strings, that can contain duplicates: <"A", "A", "B", "B", "C", "C", "D", "E", "F">

Then let's say I have some other string lists, which may or may not be subsets of the original list. I need to know:

  1. Does the second set "cover" the first set (e.g., is every item in the first set also contained in the second)?
  2. If 1 is true, how many instances of the second set does it take to recreate the first set?

So, in this case, if my second set was the list: <"A", "B", "C", "D", "E", "F">, I would get TRUE and 2.

If it was the list: <"A", "B", "C">, I would get FALSE.

If my first was <"A", "A", "A", "A", "B", "B", "B", "C", "C">:

  • Second is <"A", "B", "C">: returns TRUE and 4.
  • Second is <"A", "A", "B", "C">: returns TRUE and 3.

I know that this can be easily accomplished in N x M time using a nested loop. But I am looking for a (preferably Linq-based) solution that is concise and/or optimized. I played with Linq.Except but the problem with that is that it returns only the distinct elements, and is thereby useless when comparing lists of strings that contain duplicates.

Anyone have any unique ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2024-10-18 14:59:53

第二组是否“覆盖”第一组(例如,第一组中的每个项目也包含在第二组中)吗?

// Assuming that the elements are comparable (strings are);
// if not, need to implement your own IComparer<T>
var doesCover = !original.Except(secondList).Any();

重新创建第一个集合需要多少个第二个集合的实例?

var instancesRequired = secondList.GroupBy(e => e)
    .Max(gr => (original.Count(e => e.Equals(gr.Key))
               + gr.Count() - 1) / gr.Count());

警告:如果originalsecondList都为空,则doesCover将为true,但的计算instancesRequired 将抛出异常。因此,可能需要专门检查空列表。

Does the second set "cover" the first set (e.g., is every item in the first set also contained in the second)?

// Assuming that the elements are comparable (strings are);
// if not, need to implement your own IComparer<T>
var doesCover = !original.Except(secondList).Any();

How many instances of the second set does it take to recreate the first set?

var instancesRequired = secondList.GroupBy(e => e)
    .Max(gr => (original.Count(e => e.Equals(gr.Key))
               + gr.Count() - 1) / gr.Count());

Caveat: If both original and secondList are empty, then doesCover will be true but the calculation of instancesRequired will throw an exception. Might want to specifically check for empty lists therefore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文