[英]How to Find All Possible String Combinations Within 2 Different Size String Arrays
I am running into a peculiar problem of which i couldn't find any solution after long search. 我遇到了一个特有的问题,经过长时间搜索,我找不到任何解决方案。 Here is my question: 这是我的问题:
I have two String Arrays: 我有两个字符串数组:
var arr1 = new[] { "UserId", "BookId", "BookStoreId", "Appid" };
var arr2 = new[] {"Same", "Unique", "Repetitive"};
I want Result with all possible combinations like: 我希望结果具有所有可能的组合,例如:
Result (Should be in tabular format): 结果(应为表格格式):
Columns- UserId BookId BookStoreId Appid 列-UserId BookId BookStoreId Appid
Row3 Same Same Same Repetitive Row3同样相同的重复
Row4 Same Same Unique Same Row4同样相同唯一相同
Row5 Same Same Repetitive Same Row5相同重复相同
Row6 Same Same Unique Unique Row6相同相同唯一唯一
Row8 Same Same Repetitive Repetitive 第8行相同重复重复
Row9 Same Unique Same Same 第9行相同唯一相同
Rows and Columns are only for notation purpose. 行和列仅用于表示目的。
I hope my question is clear now. 我希望我的问题现在已经清楚了。
I Should create 64 Unique Combinations. 我应该创建64个唯一组合。
I have tried lots of ways including Cartesian products but that didnt solved my problem. 我尝试了很多方法,包括笛卡尔积,但这并没有解决我的问题。
Seems to you need a Combinations With Repetitions. 似乎您需要结合重复。 Check this answer https://stackoverflow.com/a/25824818/1007620 检查此答案https://stackoverflow.com/a/25824818/1007620
Summary: 摘要:
static IEnumerable<String> CombinationsWithRepition(IEnumerable<string> input, int length)
{
if (length <= 0)
yield return "";
else
{
foreach(var i in input)
foreach(var c in CombinationsWithRepition(input, length-1))
yield return i + ',' + c;
}
}
Then 然后
string[] items = {"UserId", "BookId", "BookStoreId", "Appid", "Same", "Unique", "Repetitive"};
foreach (var c in CombinationsWithRepition(items, 4))
Console.WriteLine (c);
Result https://pastebin.com/2hCbA1XJ 结果https://pastebin.com/2hCbA1XJ
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.