Hexspeak

Solution For Hexspeak

Hexspeak is a problem on LeetCode that requires you to convert a given integer into a string that represents the hexadecimal value of the integer. In addition, any digit that has a value greater than or equal to 10 must be replaced with a corresponding English letter using the Hexspeak alphabet “O, I, E, A, S, G, T, B”. If the integer contains any digit that cannot be represented by this alphabet, the function should return “ERROR”.

Here’s an example solution in Python:

def toHexspeak(num: str) -> str:
hex_str = hex(int(num))[2:].upper() # convert to hexadecimal
hexspeak_chars = {
'0': 'O',
'1': 'I',
'2': 'E',
'3': 'A',
'4': 'S',
'5': 'G',
'6': 'T',
'7': 'B',
'8': 'ERROR',
'9': 'ERROR',
'A': 'A',
'B': 'B',
'C': 'ERROR',
'D': 'ERROR',
'E': 'E',
'F': 'F'
}
hexspeak = ''
for char in hex_str:
if char not in hexspeak_chars:
return 'ERROR'
hexspeak += hexspeak_chars[char] return hexspeak

The function takes in an argument num, which is the integer to convert to Hexspeak. First, the function uses the built-in hex function to convert the integer to hexadecimal, and then extracts the hexadecimal value as a string.

Next, the function defines a dictionary that maps each hexadecimal digit to its corresponding Hexspeak alphabet character. It then loops through each digit in the hexadecimal string and looks up the corresponding alphabet character in the dictionary. If the digit is not a valid Hexspeak character (i.e. it is 8 or 9), the function returns “ERROR”. Otherwise, it adds the Hexspeak character to a new string, which is ultimately returned as the result.

Here are some example inputs and expected outputs:

“`

toHexspeak(‘257’)
‘IOI’
toHexspeak(‘3E8’)
‘SOT’
toHexspeak(’64’)
‘ERROR’
toHexspeak(‘10000’)
‘ERROR’
“`

Step by Step Implementation For Hexspeak

class Solution {
    public String toHexspeak(String num) {
        long n = Long.parseLong(num);
        return Long.toHexString(n).toUpperCase().replaceAll("1", "I").replaceAll("0", "O").replaceAll("B", "8").replaceAll("A", "6");
    }
}
def hexspeak(num):
    # your code here
    return None
var toHexspeak = function(num) {
    
    // convert number to hexadecimal
    let hex = num.toString(16).toUpperCase();
    
    // replace all invalid characters with corresponding letters
    let res = hex.replace(/0/g, 'O').replace(/1/g, 'I').replace(/a/g, 'A').replace(/b/g, 'B').replace(/c/g, 'C').replace(/d/g, 'D').replace(/e/g, 'E').replace(/f/g, 'F');
    
    // return result
    return res;
    
};
class Solution {
public:
    string toHexspeak(string num) {
        long n = stol(num);
        string res = "";
        while (n) {
            int cur = n % 16;
            if (cur < 10)
                res += to_string(cur);
            else
                res += (char)('a' + cur - 10);
            n /= 16;
        }
        reverse(res.begin(), res.end());
        for (char &c : res)
            if (c == '0') c = 'O';
            else if (c == '1') c = 'I';
        return res;
    }
};
public class Solution {
    public string ToHexspeak(long num) {
        // long num = 255;
        string res = "";
        
        while (num > 0)
        {
            long rem = num % 16;
            num = num / 16;
            
            if (rem == 0)
            {
                res = "O" + res;
            }
            else if (rem == 1)
            {
                res = "I" + res;
            }
            else if (rem == 10)
            {
                res = "A" + res;
            }
            else if (rem == 11)
            {
                res = "B" + res;
            }
            else if (rem == 12)
            {
                res = "C" + res;
            }
            else if (rem == 13)
            {
                res = "D" + res;
            }
            else if (rem == 14)
            {
                res = "E" + res;
            }
            else if (rem == 15)
            {
                res = "F" + res;
            }
            else
            {
                return "ERROR";
            }
        }
        
        return res;
    }
}
Scroll to Top

Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]