800. Similar RGB Color
Approach 1: Check nearest neighbours two characters at a time
For every pair of characters we will just check if their nearest shorthand characters have min sum, and keep on repeating this till we reach the end of the string
Time Complexity: O(1)
Space Complexity: O(1)
Approach 2: Use more advanced math
Since we're evaluating the decimal form of the hex pairs, it might be easier for us to find out which one of the nearest neighbours it is. It'll be either before or after the first pairwise character.
Using some quick math this math becomes evident
Time Complexity: O(1)
Space Complexity: O(1)
This solution is faster even thought the time complexity is the same because for each pair we were iterating through three neighbours to find the right value, but here it just takes one division to figure out the closest neighbour
Last updated