static IEnumerable<string> FindSub(string str, int len)
{
if (str == null || str.Length < len)
{
yield break;
}
if (len == 1)
{
foreach (var c in str)
{
yield return c.ToString();
}
yield break;
}
if (len == str.Length)
{
yield return str;
yield break;
}
for (int i = 0; i <= str.Length - len; i++)
{
var c = str[i];
foreach (var sub in FindSub(str.Substring(i + 1), len - 1))
{
yield return c + sub;
}
}
}
static bool Contains(string find, string str)
{
if (find == null || str == null || find.Length == 0 || str.Length == 0)
{
return false;
}
if (find.Length > str.Length)
{
return false;
}
int cur = 0;
for (int i = 0; i < find.Length; i++)
{
var index = str.IndexOf(find[i], cur);
if (index < 0)
{
return false;
}
cur = index + 1;
}
return true;
}
static string[] FindMax(string[] array)
{
var result = new List<string>();
if (array == null) { return result.ToArray(); }
array = array.Where(e => e != null && e.Length >= 2).OrderBy(e => e.Length).ToArray();
if (array.Length < 2) { return result.ToArray(); }
var item = array[0];
for (int newLen = item.Length; newLen >= 2; newLen--)
{
foreach (var sub in FindSub(item, newLen))
{
var match = true;
for (var index = 1; index < array.Length; index++)
{
if (!Contains(sub, array[index]))
{
match = false;
break;
}
}
if (match)
{
result.Add(sub);
}
}
if (result.Count > 0)
{
return result.ToArray();
}
}
return result.ToArray();
}
public static void Main(string[] args)
{
var items = @"[
[""132"",""123"",""112233""],
[""1523"",""123"",""112233""],
[""172853"",""8516623"",""053122883""],
[""123"",""132"",""321""],
[""123"",""132"",""3212""],
[""64532"",""8366252183"",""5266435412""],
[""132"",""123"",""112233""]
]";
var arrays = JsonConvert.DeserializeObject<string[][]>(items);
foreach (var array in arrays)
{
Console.WriteLine($"{string.Join(",", array)} => {string.Join(",", FindMax(array))}");
}
Console.ReadLine();
}
|