Skip to content

Instantly share code, notes, and snippets.

@lucasteles
Forked from WennderSantos/RunLength.cs
Last active November 2, 2018 01:28
Show Gist options
  • Save lucasteles/5cd463f69f61f469b88e22576490a452 to your computer and use it in GitHub Desktop.
Save lucasteles/5cd463f69f61f469b88e22576490a452 to your computer and use it in GitHub Desktop.
RunLength. O(n)
using static System.Console;
using System.Linq;
public static class Program {
public static void Main(string[] args)
{
var input = "wwwwaaadexxxxxxywww";
WriteLine(input);
var counts =
input
.Aggregate(
(text: "", last: '\0', count: 0),
(acc, c) =>
c == acc.last || acc.count == 0
? (acc.text, c, acc.count+1)
: ($"{acc.text} {acc.last}{acc.count}", c, 1)
)
.text;
Write(counts); //4w 3a 1d 1e 6x 1y 3w
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment