Similar RGB Color - Solution & Explanation
Problem Statement
The red-green-blue color "#AABBCC" can be written as "#ABC" in shorthand.
- For example,
"#15c"is shorthand for the color"#1155cc".
The similarity between the two colors "#ABCDEF" and "#UVWXYZ" is -(AB - UV)2 - (CD - WX)2 - (EF - YZ)2.
Given a string color that follows the format "#ABCDEF", return a string 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.
Example 1:
Input: color = "#09f166" Output: "#11ee66" Explanation: The similarity is -(0x09 - 0x11)2 -(0xf1 - 0xee)2 - (0x66 - 0x66)2 = -64 -9 -0 = -73. This is the highest among any shorthand color.
Example 2:
Input: color = "#4e3fe1" Output: "#5544dd"
Constraints:
color.length == 7color[0] == '#'color[i]is either digit or character in the range['a', 'f']fori > 0.
Approach Overview
Problem Overview: You receive a 6‑digit hexadecimal RGB color such as #09f166. The task is to return the most similar shorthand RGB color where each channel uses repeated digits (for example #11ee66, #00ff00). Similarity is defined by minimizing the squared distance between the original and candidate RGB values.
Approach 1: Brute Force Enumeration (O(1) time, O(1) space)
Shorthand RGB colors have the format #XY where each channel repeats the same hex digit: 00, 11, 22, ..., ff. That gives 16 possible values per channel and 16^3 = 4096 total shorthand colors. Iterate through every possible shorthand combination, compute the squared RGB difference from the input color, and keep the minimum. Each comparison extracts the numeric values of the red, green, and blue components and evaluates (r1-r2)^2 + (g1-g2)^2 + (b1-b2)^2. Because the search space is fixed, the complexity is constant, but the implementation is more verbose.
This approach relies on straightforward enumeration and simple numeric conversion from hexadecimal strings. It works well as a baseline because it directly mirrors the problem definition.
Approach 2: Nearest Multiple of 17 (O(1) time, O(1) space)
A shorthand component like aa actually represents 17 * value_of(a) in decimal. For example 11 = 17, 22 = 34, and ff = 255. This means every shorthand channel is a multiple of 17. Instead of enumerating all colors, process each RGB channel independently: convert the two‑digit hex value to decimal, divide by 17, round to the nearest integer, then map back to a repeated hex digit.
This works because minimizing the squared distance across RGB channels is equivalent to minimizing each channel independently. The rounding step effectively chooses the closest shorthand value among 0, 17, 34, ..., 255. Implementation involves basic math operations and simple string manipulation to rebuild the final hex color.
Recommended for interviews: The nearest‑multiple‑of‑17 method is the expected solution. It demonstrates pattern recognition and avoids unnecessary enumeration. Mentioning the brute force approach first shows you understand the search space, but deriving the mathematical shortcut shows stronger problem‑solving ability.
Solution
Try this approach in the editor →Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration of All Shorthand Colors | O(1) (4096 checks) | O(1) | When implementing the most direct interpretation of the problem or validating correctness. |
| Nearest Multiple of 17 per Channel | O(1) | O(1) | Best approach for interviews and production code. Uses math insight to compute the closest shorthand color directly. |
Video Solution
800. Similar RGB Color (Leetcode Easy) • Programming Live with Larry • 301 views views
Watch 4 more video solutions →Frequently Asked Questions
Is Similar RGB Color easy or hard?
Similar RGB Color Python/Java solution
How to solve Similar RGB Color in O(1)?
What is the best approach for Similar RGB Color?
Is Similar RGB Color asked at Google/Amazon/Meta?
What data structure is used in Similar RGB Color?
What is the time complexity of Similar RGB Color?
Ready to solve this problem?
Practice Similar RGB Color with our built-in code editor and test cases.
Practice on FleetCode