Hexspeak - 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 the Hexspeak representation of n if it is valid, otherwise return "ERROR".
Input & Output
Example 1 — Valid Hexspeak
$
Input:
num = "257"
›
Output:
"IOI"
💡 Note:
257 in hex is 101. Replace '1'→'I' and '0'→'O' gives 'IOI'. All characters {I,O} are valid hexspeak.
Example 2 — Invalid Hexspeak
$
Input:
num = "3"
›
Output:
"ERROR"
💡 Note:
3 in hex is 3. The digit '3' cannot be converted to valid hexspeak (not in {A,B,C,D,E,F,I,O}).
Example 3 — Large Valid Number
$
Input:
num = "171"
›
Output:
"AB"
💡 Note:
171 in hex is AB. Both 'A' and 'B' are already valid hexspeak characters.
Constraints
- 1 ≤ num ≤ 1012
- num consists of only digits
Visualization
Tap to expand
Understanding the Visualization
1
Input
Decimal number as string
2
Convert
Transform to hex and apply hexspeak rules
3
Validate
Check if result contains only valid characters
Key Takeaway
🎯 Key Insight: Only specific hex digits (0,1,A-F) can form valid hexspeak after transformation
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code