class Solution:
def toHexspeak(self, num: str) -> str:
n = int(num)
hex_str = hex(n)[2:].upper()
result = []
for ch in hex_str:
if ch == '0':
result.append('O')
elif ch == '1':
result.append('I')
elif ch in 'ABCDEF':
result.append(ch)
else:
return "ERROR"
return ''.join(result)
class Solution {
public:
string toHexspeak(string num) {
long long n = stoll(num);
string hex = "";
while (n > 0) {
int rem = n % 16;
if (rem == 0)
hex += 'O';
else if (rem == 1)
hex += 'I';
else if (rem >= 10 && rem <= 15)
hex += 'A' + (rem - 10);
else
return "ERROR";
n /= 16;
}
reverse(hex.begin(), hex.end());
return hex;
}
};
class Solution {
public String toHexspeak(String num) {
long n = Long.parseLong(num);
StringBuilder sb = new StringBuilder();
while (n > 0) {
long rem = n % 16;
if (rem == 0)
sb.append('O');
else if (rem == 1)
sb.append('I');
else if (rem >= 10 && rem <= 15)
sb.append((char)('A' + (rem - 10)));
else
return "ERROR";
n /= 16;
}
return sb.reverse().toString();
}
}
var toHexspeak = function(num) {
let n = BigInt(num);
let hex = n.toString(16).toUpperCase();
let result = '';
for (let ch of hex) {
if (ch === '0') result += 'O';
else if (ch === '1') result += 'I';
else if ('ABCDEF'.includes(ch)) result += ch;
else return "ERROR";
}
return result;
};
Given a decimal string num
representing a positive integer, you are required to convert it to a "Hexspeak" string.
The process involves:
num
to its hexadecimal representation (without the 0x
prefix).0
with O
and every 1
with I
.A
to F
).0
, 1
, or A
to F
), return "ERROR"
.Only one valid output is possible for each input. You do not need to reuse or rearrange any elements.
At first glance, the problem seems to be a simple base conversion task. However, the twist comes from the "Hexspeak" rules: only certain digits are allowed in the output, and others (like 2-9) are forbidden.
A brute-force approach would be to convert the number to hexadecimal, then check each character and build the Hexspeak string if possible. If any forbidden digit appears, we immediately return "ERROR"
.
Optimization is straightforward because:
Let's break down the steps to solve the problem:
hex()
in Python, toString(16)
in JavaScript).0x
if present, and convert to uppercase.0
, replace it with O
.1
, replace it with I
.A
to F
, keep it as is.2
to 9
), return "ERROR"
.This approach is efficient and easy to implement. We use simple string operations and a single pass through the hexadecimal digits, making it both readable and fast.
Let's walk through an example to see how the solution works step by step.
Example Input: num = "257"
101
1
→ I
0
→ O
1
→ I
IOI
Another Example: num = "3"
3
, which is not allowed in Hexspeak."ERROR"
.Brute-force Approach:
O(L)
, where L
is the length of the hexadecimal string.
O(L)
for storing the result.
O(L)
(one pass through the hexadecimal string).
O(L)
(for the output string).
There is no significant difference between brute-force and optimized approaches due to the simplicity of the task.
The Hexspeak problem is a straightforward exercise in number base conversion and string manipulation. By leveraging built-in hexadecimal conversion and simple character checks, we can efficiently determine if a number's hex representation can be transformed into Hexspeak. The key insight is to validate each digit and perform substitutions as specified, returning "ERROR"
immediately if any invalid digit is found. The solution is clean, efficient, and easy to understand, making it a great example of applying basic programming concepts to meet unique problem constraints.