Skip to main content

Similar RGB Color - Solution & Explanation

EasyPremiumFree on FleetCodeMathStringEnumeration4 min readAsked at: Google
Practice this problem

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 == 7
  • color[0] == '#'
  • color[i] is either digit or character in the range ['a', 'f'] for i > 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

Code

Python

Java

Go

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Enumeration of All Shorthand ColorsO(1) (4096 checks)O(1)When implementing the most direct interpretation of the problem or validating correctness.
Nearest Multiple of 17 per ChannelO(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 Larry301 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Similar RGB Color easy or hard?
Similar RGB Color is categorized as an Easy problem. The key challenge is recognizing that shorthand RGB values are multiples of 17, which enables a direct mathematical solution instead of brute force enumeration.
Similar RGB Color Python/Java solution
A typical Python or Java solution extracts the three color channels, converts them from hexadecimal to integers, rounds each value to the nearest multiple of 17, then formats the repeated hex digit back into the final color string. The algorithm runs in constant time.
How to solve Similar RGB Color in O(1)?
Convert each two‑digit hex channel to decimal, divide by 17, and round to the nearest integer. That integer corresponds to the shorthand hex digit (0–f). Repeat the digit twice to form the shorthand component, and combine the three components into the final color string.
What is the best approach for Similar RGB Color?
The optimal approach rounds each RGB channel to the nearest multiple of 17. Shorthand RGB colors repeat the same hex digit (00, 11, 22 ... ff), and each of these values equals 17 multiplied by a digit from 0–15. Converting each channel to decimal, dividing by 17, rounding, and converting back gives the closest shorthand color in O(1) time.
Is Similar RGB Color asked at Google/Amazon/Meta?
Similar RGB Color is a LeetCode easy problem commonly used to test basic math reasoning, hexadecimal manipulation, and string handling. Problems involving color encoding or rounding patterns appear in interviews at large tech companies such as Google and Amazon.
What data structure is used in Similar RGB Color?
The solution mainly relies on string parsing and basic arithmetic. No complex data structures are required. You convert hexadecimal substrings to integers, perform math rounding, and reconstruct the result as a string.
What is the time complexity of Similar RGB Color?
The optimized mathematical approach runs in O(1) time and O(1) space because it processes exactly three RGB channels with constant work. A brute force alternative enumerates 4096 shorthand colors (16^3), which is also constant time but less efficient in practice.

Ready to solve this problem?

Practice Similar RGB Color with our built-in code editor and test cases.

Practice on FleetCode