Skip to the code
cf.
+25 XP

#206 Sort a Palette of Hex Colors

medium

A design tool exports a palette as hex color codes like "#a1b2c3": a # followed by exactly six characters, each a digit 0-9 or a lowercase letter a-f. The export must list the codes in dictionary order, comparing character codes from left to right.

Write sortPalette(codes) that returns a new array with the codes in that order, keeping duplicates. Because every code has the same length, you can sort them with least-significant-character radix sort: one stable pass per character position, from the last character to the first.

Example 1

Input: codes = ["#ff0000","#00ff00","#0000ff"]
Output: ["#0000ff","#00ff00","#ff0000"]

Digits come before letters, and 0 comes before f.

Example 2

Input: codes = ["#abcdef","#abcdee"]
Output: ["#abcdee","#abcdef"]

They differ only in the last character.

Constraints

  • 0 <= codes.length <= 100000
  • Each code matches # plus six characters from 0-9a-f.
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).