Skip to the code
cf.
+10 XP

#622 Compress a Pixel Row

easy

A pixel-art editor saves each row of the canvas compactly. A row is a list of color names, and long stretches of one color are common.

Write compressRow(colors) that returns a list of [color, count] pairs, one for each run of equal neighboring colors, in order. An empty row gives an empty list.

Example 1

Input: colors = ["white","white","white","red","white","white"]
Output: [["white",3],["red",1],["white",2]]

Three runs: three white, one red, two white.

Example 2

Input: colors = ["blue"]
Output: [["blue",1]]

One pixel is one run of length 1.

Constraints

  • 0 <= colors.length <= 10^5
  • Each color is a non-empty string.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).