TypeScriptMediumStrings25 min

Compress consecutive character runs

For each consecutive run of ASCII letters, output its character followed by its decimal count, including counts of one. Inputs are valid JSON and must not be modified unless the algorithm uses a local copy. This exercise probes typed data modeling, boundary handling, and algorithm selection.

Input

One JSON value: string. See the sample for its exact shape.

Output

One compact JSON value of type string. Strings include JSON quotes; arrays preserve the specified order.

Constraints

Array/string lengths are 0 to 10000 unless stated otherwise. Numbers are integers in [-1000000,1000000]; all computed results fit the safe integer range.

Example

Input

"aabccc"

Output

"a2b1c3"

Example 2 input

"aAAa"

Output

"a1A2a1"

Checks a boundary or contrasting case beyond the public examples.

Public test cases (2)

  • Input

    "aabccc"

    Expected output

    "a2b1c3"
  • Input

    ""

    Expected output

    ""
Hints (2)
  1. Start with the required output order and the empty-input behavior.
  2. Advance to the end of each maximal equal-character run

Target complexity: time O(n) · space O(n)

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All TypeScript programs