I would say the first place to start here is to generate a list of all the possible substrings from the input of length 2 to the length of the input:
string text = "xfoofoobarbar fooxxfoo barxxxfoo";
var allSubstrings = Enumerable.Range(2,text.Length)
.ToDictionary(k => k,v => FindSubStrings(text,v));
...
IEnumerable<string> FindSubStrings(string input, int length)
{
for(var i=0;i<input.Length-length;i++)
{
yield return input.Substring(i,length);
}
}
Live example: http://rextester.com/ZUR68480
From there it should be as simple as grouping by the substring to get a count, and ordering the result appropriately. But your requirements seem to pick and choose between "longest length" and "most occurrences", you cant have both!
Here is my full implementation, which I should point out chooses xfoo
as the winner at present.
public static void Main(string[] args)
{
string text = "xfoofoobarbar fooxxfoo barxxxfoo";
var allSubstrings = Enumerable.Range(2,text.Length-2)
.Select(x => {
var longestSub = FindSubStrings(text,x).GroupBy(y => y).OrderByDescending(y => y.Count()).FirstOrDefault();
return new Substrings {
Length = x,
Count = longestSub.Count(),
Value = longestSub.Key
};
});
foreach(var item in allSubstrings)
{
Console.WriteLine(item.Length + ":" + item.Count + ":" + item.Value);
}
var best = allSubstrings.Where(x => x.Count>1).OrderByDescending(x => x.Length).ThenByDescending(x => x.Count).First();
Console.WriteLine("Longest, most frequest substring is " + best.Value);
}
public class Substrings
{
public int Length{get;set;}
public int Count{get;set;}
public string Value{get;set;}
}
private static IEnumerable<string> FindSubStrings(string input, int length)
{
for(var i=0;i<input.Length-length;i++)
{
yield return input.Substring(i,length);
}
}
Live example: http://rextester.com/RJNP55827