Similar RGB Color - Problem
The red-green-blue color #AABBCC can be written as #ABC in shorthand when each pair has identical characters. For example, #15c is shorthand for the color #1155cc.
The similarity between two colors #ABCDEF and #UVWXYZ is calculated as: -(AB - UV)² - (CD - WX)² - (EF - YZ)².
Given a string color that follows the format #ABCDEF, return a string that represents the color that is most similar to the given color and has a shorthand (i.e., it can be represented as some #XYZ).
Any answer which has the same highest similarity as the best answer will be accepted.
Input & Output
Example 1 — Basic Case
$
Input:
color = "#09f24a"
›
Output:
"#1e4"
💡 Note:
RGB components are (9, 242, 74). Closest shorthand digits: R→1 (17 vs 9, diff=8), G→e (238 vs 242, diff=4), B→4 (68 vs 74, diff=6). Result: #1e4
Example 2 — Already Shorthand
$
Input:
color = "#aabbcc"
›
Output:
"#abc"
💡 Note:
Input is already in shorthand-compatible format. RGB (170, 187, 204) maps perfectly to shorthand #abc
Example 3 — Dark Color
$
Input:
color = "#123456"
›
Output:
"#123"
💡 Note:
RGB components (18, 52, 86). Closest shorthand: R→1 (17 vs 18), G→3 (51 vs 52), B→5 (85 vs 86). Very close matches.
Constraints
- color is a valid 6-digit hexadecimal color string
- color follows the format "#ABCDEF" where A, B, C, D, E, F are hexadecimal digits
- Return value should be in format "#XYZ" where X, Y, Z are hexadecimal digits
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code