##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/800.html

# 800. Similar RGB Color (Easy)

In the following, every capital letter represents some hexadecimal digit from 0 to f.

The red-green-blue color "#AABBCC" can be written as "#ABC" in shorthand.  For example, "#15c" is shorthand for the color "#1155cc".

Now, say the similarity between two colors "#ABCDEF" and "#UVWXYZ" is -(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2.

Given the color "#ABCDEF", return a 7 character color that is most similar to #ABCDEF, and has a shorthand (that is, it can be represented as some "#XYZ"

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.


Note:

• color is a string of length 7.
• color is a valid RGB color: for i > 0, color[i] is a hexadecimal digit from 0 to f
• Any answer which has the same (highest) similarity as the best answer will be accepted.
• All inputs and outputs should use lowercase letters, and the output is 7 characters.

Companies:

Related Topics:
Math, String

## Solution 1.

• class Solution {
public String similarRGB(String color) {
int red = Integer.parseInt(color.substring(1, 3), 16);
int green = Integer.parseInt(color.substring(3, 5), 16);
int blue = Integer.parseInt(color.substring(5, 7), 16);
int closestRed = 0, closestGreen = 0, closestBlue = 0;
for (int i = 0; i <= 0xff; i += 0x11) {
int redDifference = Math.abs(red - i);
int greenDifference = Math.abs(green - i);
int blueDifference = Math.abs(blue - i);
if (redDifference < Math.abs(red - closestRed))
closestRed = i;
if (greenDifference < Math.abs(green - closestGreen))
closestGreen = i;
if (blueDifference < Math.abs(blue - closestBlue))
closestBlue = i;
}
String similarRGB = "#" + toHex(closestRed) + toHex(closestGreen) + toHex(closestBlue);
return similarRGB;
}

public String toHex(int num) {
String hex = "";
while (num > 0) {
int remainder = num % 16;
String curHex = remainder < 10 ? String.valueOf(remainder) : String.valueOf((char) (remainder - 10 + 'a'));
hex = curHex + hex;
num /= 16;
}
while (hex.length() < 2)
hex = "0" + hex;
return hex;
}
}

############

class Solution {
public String similarRGB(String color) {
String a = color.substring(1, 3), b = color.substring(3, 5), c = color.substring(5, 7);
return "#" + f(a) + f(b) + f(c);
}

private String f(String x) {
int q = Integer.parseInt(x, 16);
q = q / 17 + (q % 17 > 8 ? 1 : 0);
return String.format("%02x", 17 * q);
}
}

• // OJ: https://leetcode.com/problems/similar-rgb-color/
// Time: O(1)
// Space: O(1)
class Solution {
private:
const string digits = "0123456789abcdef";
public:
string similarRGB(string color) {
string ans = "#";
for (int i = 0; i < 3; ++i) {
char a = color[1 + i * 2], b = color[2 + i * 2];
int hex1 = stoi(color.substr(1 + i * 2, 2), nullptr, 16);
int best = 0, bestSim = INT_MIN;
for (int j = 0; j < 16; ++j) {
int hex2 = j * 16 + j;
int sim = -pow(hex1 - hex2, 2);
if (sim > bestSim) {
bestSim = sim;
best = j;
}
}
char c = digits[best];
ans += c;
ans += c;
}
return ans;
}
};

• class Solution:
def similarRGB(self, color: str) -> str:
def f(x):
y, z = divmod(int(x, 16), 17)
if z > 8:
y += 1
return '{:02x}'.format(17 * y)

a, b, c = color[1:3], color[3:5], color[5:7]
return f'#{f(a)}{f(b)}{f(c)}'


• func similarRGB(color string) string {
f := func(x string) string {
q, _ := strconv.ParseInt(x, 16, 64)
if q%17 > 8 {
q = q/17 + 1
} else {
q = q / 17
}
return fmt.Sprintf("%02x", 17*q)

}
a, b, c := color[1:3], color[3:5], color[5:7]
return "#" + f(a) + f(b) + f(c)
}