Problem

A decimal number can be converted to its Hexspeak representation by first converting it to an uppercase hexadecimal string, then replacing all occurrences of the digit '0' with the letter 'O', and the digit '1' with the letter 'I'. Such a representation is valid if and only if it consists only of the letters in the set {'A', 'B', 'C', 'D', 'E', 'F', 'I', 'O'}.

Given a string num representing a decimal integer n, _return theHexspeak representation of _n if it is valid, otherwise return"ERROR".

Examples

Example 1:

1
2
3
Input: num = "257"
Output: "IOI"
Explanation: 257 is 101 in hexadecimal.

Example 2:

1
2
Input: num = "3"
Output: "ERROR"

Constraints:

  • 1 <= num.length <= 12
  • num does not contain leading zeros.
  • num represents an integer in the range [1, 1012].

Solution

Method 1 – Hex Conversion and Validation

Intuition

Convert the number to uppercase hexadecimal, replace ‘0’ with ‘O’ and ‘1’ with ‘I’, and check if all characters are in the allowed set. If so, return the result; otherwise, return “ERROR”.

Approach

  1. Convert the decimal string to an integer.
  2. Convert the integer to an uppercase hexadecimal string.
  3. Replace ‘0’ with ‘O’ and ‘1’ with ‘I’.
  4. Check if all characters are in {‘A’,‘B’,‘C’,‘D’,‘E’,‘F’,‘I’,‘O’}.
  5. If valid, return the string; else, return “ERROR”.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
    string toHexspeak(string num) {
        long long n = stoll(num);
        string hex;
        while (n) {
            int d = n % 16;
            if (d == 0) hex += 'O';
            else if (d == 1) hex += 'I';
            else if (d >= 10 && d <= 15) hex += 'A' + (d - 10);
            else return "ERROR";
            n /= 16;
        }
        reverse(hex.begin(), hex.end());
        return hex;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import "strings"
func toHexspeak(num string) string {
    n := new(big.Int)
    n.SetString(num, 10)
    hex := ""
    for n.Cmp(big.NewInt(0)) > 0 {
        d := new(big.Int)
        n.DivMod(n, big.NewInt(16), d)
        v := int(d.Int64())
        if v == 0 {
            hex = "O" + hex
        } else if v == 1 {
            hex = "I" + hex
        } else if v >= 10 && v <= 15 {
            hex = string('A'+(v-10)) + hex
        } else {
            return "ERROR"
        }
    }
    return hex
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    public String toHexspeak(String num) {
        long n = Long.parseLong(num);
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            int d = (int)(n % 16);
            if (d == 0) sb.append('O');
            else if (d == 1) sb.append('I');
            else if (d >= 10 && d <= 15) sb.append((char)('A' + d - 10));
            else return "ERROR";
            n /= 16;
        }
        return sb.reverse().toString();
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
    fun toHexspeak(num: String): String {
        var n = num.toLong()
        val sb = StringBuilder()
        while (n > 0) {
            val d = (n % 16).toInt()
            when (d) {
                0 -> sb.append('O')
                1 -> sb.append('I')
                in 10..15 -> sb.append('A' + (d - 10))
                else -> return "ERROR"
            }
            n /= 16
        }
        return sb.reverse().toString()
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def toHexspeak(self, num: str) -> str:
        n = int(num)
        hexs = ''
        while n > 0:
            d = n % 16
            if d == 0:
                hexs = 'O' + hexs
            elif d == 1:
                hexs = 'I' + hexs
            elif 10 <= d <= 15:
                hexs = chr(ord('A') + d - 10) + hexs
            else:
                return "ERROR"
            n //= 16
        return hexs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
struct Solution;
impl Solution {
    pub fn to_hexspeak(num: String) -> String {
        let mut n = num.parse::<u64>().unwrap();
        let mut hex = String::new();
        while n > 0 {
            let d = n % 16;
            if d == 0 {
                hex.push('O');
            } else if d == 1 {
                hex.push('I');
            } else if d >= 10 && d <= 15 {
                hex.push((b'A' + (d - 10) as u8) as char);
            } else {
                return "ERROR".to_string();
            }
            n /= 16;
        }
        hex.chars().rev().collect()
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    toHexspeak(num: string): string {
        let n = BigInt(num);
        let hex = '';
        while (n > 0n) {
            const d = Number(n % 16n);
            if (d === 0) hex = 'O' + hex;
            else if (d === 1) hex = 'I' + hex;
            else if (d >= 10 && d <= 15) hex = String.fromCharCode(65 + d - 10) + hex;
            else return "ERROR";
            n = n / 16n;
        }
        return hex;
    }
}

Complexity

  • ⏰ Time complexity: O(log n), where n is the value of the number, since we process each hex digit.
  • 🧺 Space complexity: O(1), ignoring the output string.