|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +/** |
| 8 | + * 800. Similar RGB Color |
| 9 | +
|
| 10 | + In the following, every capital letter represents some hexadecimal digit from 0 to f. |
| 11 | +
|
| 12 | + The red-green-blue color "#AABBCC" can be written as "#ABC" in shorthand. For example, "#15c" is shorthand for the color "#1155cc". |
| 13 | +
|
| 14 | + Now, say the similarity between two colors "#ABCDEF" and "#UVWXYZ" is -(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2. |
| 15 | +
|
| 16 | + 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" |
| 17 | +
|
| 18 | + Example 1: |
| 19 | + Input: color = "#09f166" |
| 20 | + Output: "#11ee66" |
| 21 | + Explanation: |
| 22 | + The similarity is -(0x09 - 0x11)^2 -(0xf1 - 0xee)^2 - (0x66 - 0x66)^2 = -64 -9 -0 = -73. |
| 23 | + This is the highest among any shorthand color. |
| 24 | +
|
| 25 | + Note: |
| 26 | +
|
| 27 | + color is a string of length 7. |
| 28 | + color is a valid RGB color: for i > 0, color[i] is a hexadecimal digit from 0 to f |
| 29 | + Any answer which has the same (highest) similarity as the best answer will be accepted. |
| 30 | + All inputs and outputs should use lowercase letters, and the output is 7 characters. |
| 31 | +
|
| 32 | + */ |
| 33 | +public class _800 { |
| 34 | + public static class Solution1 { |
| 35 | + public String similarRGB(String color) { |
| 36 | + List<String> allShortHandCombinations = computeAllShorthandCombinations(); |
| 37 | + int minSimilarity = Integer.MIN_VALUE; |
| 38 | + String result = ""; |
| 39 | + for (String candidate : allShortHandCombinations) { |
| 40 | + int similarity = computeSimilarity(candidate, color); |
| 41 | + if (similarity > minSimilarity) { |
| 42 | + result = candidate; |
| 43 | + minSimilarity = similarity; |
| 44 | + } |
| 45 | + } |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + private int computeSimilarity(String candidate, String color) { |
| 50 | + return -(Integer.parseInt(candidate.substring(1, 3), 16) - Integer.parseInt( |
| 51 | + color.substring(1, 3), 16)) * (Integer.parseInt(candidate.substring(1, 3), 16) |
| 52 | + - Integer.parseInt(color.substring(1, 3), 16)) |
| 53 | + - (Integer.parseInt(candidate.substring(3, 5), 16) - Integer.parseInt( |
| 54 | + color.substring(3, 5), 16)) * (Integer.parseInt(candidate.substring(3, 5), 16) |
| 55 | + - Integer.parseInt(color.substring(3, 5), 16)) |
| 56 | + - (Integer.parseInt(candidate.substring(5, 7), 16) - Integer.parseInt( |
| 57 | + color.substring(5, 7), 16)) * (Integer.parseInt(candidate.substring(5, 7), 16) |
| 58 | + - Integer.parseInt(color.substring(5, 7), 16)); |
| 59 | + } |
| 60 | + |
| 61 | + private List<String> computeAllShorthandCombinations() { |
| 62 | + List<String> result = new ArrayList<>(); |
| 63 | + List<Character> hexNumber = new ArrayList<>( |
| 64 | + Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', |
| 65 | + 'f')); |
| 66 | + for (int i = 0; i < hexNumber.size(); i++) { |
| 67 | + for (int j = 0; j < hexNumber.size(); j++) { |
| 68 | + for (int k = 0; k < hexNumber.size(); k++) { |
| 69 | + StringBuilder sb = new StringBuilder(); |
| 70 | + sb.append("#"); |
| 71 | + sb.append(hexNumber.get(i)); |
| 72 | + sb.append(hexNumber.get(i)); |
| 73 | + sb.append(hexNumber.get(j)); |
| 74 | + sb.append(hexNumber.get(j)); |
| 75 | + sb.append(hexNumber.get(k)); |
| 76 | + sb.append(hexNumber.get(k)); |
| 77 | + result.add(sb.toString()); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return result; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments