-
-
Save lucasteles/5cd463f69f61f469b88e22576490a452 to your computer and use it in GitHub Desktop.
RunLength. O(n)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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