Problem statement

https://leetcode.com/problems/hexspeak/

Solution

First, convert number to hexadecimal base, then traverse digit by digit and replace them if needed.

Complexity

Time and space complexity is O(log n).

Code

class Solution:
    def toHexspeak(self, num):
        num16 = hex(int(num))[2:]
        ans = ""
        for s in num16:
            if "2" <= s <= "9": return "ERROR"
            if s == "0": ans += "O"
            elif s == "1": ans += "I"
            else: ans += s.upper()
                
        return ans