Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Run-length code

    EasyAbout 15 minFreeLearn it: The C# Angle: Collection Costs, LINQ Costs, Strings and Span in Coding Interviews

    The task

    A sensor sends one status letter per second over a slow link, and long stretches of the same letter waste bandwidth.

    Complete RunLength(string text). Replace every run of equal neighbouring characters by the character followed by the length of the run, written in decimal. Letter case matters, and digits and spaces are characters like any other.

    Signature

    public static string RunLength(string text)

    Examples

    • "aabbbaa" → "a2b3a2". Two a's, three b's, then two a's again: a new run starts after the b's.
    • "abc" → "a1b1c1". Each character is a run of one.

    Limits: Up to 300,000 characters.

    The tests check: an empty text, one character, a run of 12 (a count with two digits), letter case, digits and spaces as characters, and a 300,000-character text in time.

    Hints

      The solution opens by itself when all tests pass. You remember more if you try first.

      Sign in to see the solution

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // Replaces each run of equal neighbouring characters by the character and the run's length.
          public static string RunLength(string text)
          {
              throw new NotImplementedException();
          }
      }

      Sign in to run and submit your code. It stays here while you do.