fork(1) download
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. Console.WriteLine(Compress("aaaabbbcccddasdfasdf"));
  10. Console.WriteLine(Compress("aaabbccdddeezbbb"));
  11. }
  12.  
  13. public static string Compress(string str)
  14. {
  15. return Regex.Matches(str, @"(.)\1{0,}")
  16. .Cast<Match>()
  17. .Select(x =>
  18. "" + x.Value[0] +
  19. ((x.Length == 1) ? String.Empty : x.Length.ToString()))
  20. .Aggregate((a, b) => a + b);
  21. }
  22. }
Success #stdin #stdout 0.11s 24416KB
stdin
Standard input is empty
stdout
a4b3c3d2asdfasdf
a3b2c2d3e2zb3