C# (LinQPad)
146
This is tsavino's answer but shorter. Here, I used Distinct()
instead of GroupBy(c=>c)
. Also the curly braces from the foreach-loop
are left out:
void v(string i){foreach(var c in i.Distinct())Console.WriteLine(c+"="+(from Match m in Regex.Matches(i,"["+c+"]+")select m.Value.Length).Max());}
136
I tried using a lambda expression
instead of the normal query syntax but since I needed a Cast<Match>
first, the code became 1 character longer... Anyhow, since it can be executed in LinQPad, you can use Dump()
instead of Console.WriteLine()
:
void v(string i){foreach(var c in i.Distinct())(c+"="+(from Match m in Regex.Matches(i,"["+c+"]+")select m.Value.Length).Max()).Dump();}
Further study of the code got me thinking about the Max()
. This function also accepts a Func
. This way I could skip the Select
part when using the lambda epxression:
void v(string i){foreach(var c in i.Distinct())(c+"="+Regex.Matches(i,"["+c+"]+").Cast<Match>().Max(m=>m.Value.Length)).Dump();}
Thus, final result:
128
Update:
Thanks to the tip from Dan Puzey, I was able to save another 6 characters:
void v(string i){i.Distinct().Select(c=>c+"="+Regex.Matches(i,"["+c+"]+").Cast<Match>().Max(m=>m.Value.Length)).Dump();}
Length:
122