606. Similar RGB Color

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 defined as:

−(AB−UV)2−(CD−WX)2−(EF−YZ)2

Given a string color that follows the format "#ABCDEF", return a string representing 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 that has the same highest similarity as the best answer will be accepted.

Example 1:

Input: color = "#12a3bc"

Output: "#11aacc"

Explanation:

The similarity is −(0x12−0x11)2−(0xa3−0xaa)2−(0xbc−0xcc)2-(0x12 - 0x11)2 - (0xa3 - 0xaa)2 - (0xbc - 0xcc)2 = -1 - 49 - 16 = -66, which is the highest among any shorthand color.

Example 2:

Input: color = "#5f7d2e"

Output: "#669922"

Explanation:

The similarity is −(0x5f−0x66)2−(0x7d−0x99)2−(0x2e−0x22)2-(0x5f - 0x66)2 - (0x7d - 0x99)2 - (0x2e - 0x22)2 = -9 - 196 - 4 = -209, which is the highest among any shorthand color.

Now Your Turn!

Pick the correct output for the given input

Input: color = "#c82391"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • color.length == 7
  • color[0] == '#'
  • color[i] is either a digit or a character in the range ['a', 'f'] for i > 0.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
string similarRGB(string color) {
// Your code goes here
}
};
Test Case

Input:

Color