Watch 2 video solutions for Hexspeak, a easy level problem involving Math, String. This walkthrough by Sleepy Cracker has 113 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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".
Example 1:
Input: num = "257" Output: "IOI" Explanation: 257 is 101 in hexadecimal.
Example 2:
Input: num = "3" Output: "ERROR"
Constraints:
1 <= num.length <= 12num does not contain leading zeros.[1, 1012].Problem Overview: You receive a decimal number as a string. Convert it to hexadecimal, then transform the result into a special format called Hexspeak. In Hexspeak, digits 0 and 1 become letters O and I. Characters A–F stay the same. Any remaining digits (2-9) make the result invalid and you must return ERROR.
Approach 1: Built-in Hex Conversion + Simulation (O(n) time, O(n) space)
The direct solution converts the decimal string to a number, then generates its hexadecimal representation using built-in language utilities. After conversion, iterate through the hex string character by character. Replace '0' with 'O', replace '1' with 'I', and keep 'A'–'F' unchanged. If any digit from '2' to '9' appears, the string cannot form valid Hexspeak, so return ERROR immediately.
This works because hexadecimal already restricts characters to 0-9 and A-F. The validation step simply filters out digits that Hexspeak does not allow. The algorithm performs a single pass over the converted string, making it efficient and easy to implement in any language. The logic is essentially a small simulation of the transformation rules using basic string processing operations.
Approach 2: Manual Base-16 Conversion + Simulation (O(n) time, O(n) space)
Instead of relying on a built-in hex conversion, you can manually convert the number to base 16 using repeated division. Treat the input as a large integer (or convert it if it fits in standard types). Repeatedly divide the number by 16, store the remainder, and map each remainder to its hexadecimal character. Build the hex string in reverse order.
During or after building the hex representation, apply the same Hexspeak mapping rules: 0 → O, 1 → I, A–F remain unchanged, and digits 2–9 invalidate the result. This approach highlights the base conversion mechanics behind hexadecimal numbers and avoids language-specific helpers.
Although slightly more verbose, it demonstrates understanding of numeric base systems and digit extraction. The complexity remains linear in the number of hexadecimal digits because each division step produces one digit.
Recommended for interviews: The built-in conversion plus simulation approach is what most interviewers expect. It shows you recognize the problem as a straightforward transformation using string iteration and simple mapping rules. Mentioning the manual base-16 conversion demonstrates deeper understanding of number systems, but implementing it is usually unnecessary unless the interviewer specifically asks for it.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Built-in Hex Conversion + Simulation | O(n) | O(n) | Best general solution. Simple implementation using built-in hex conversion and one pass validation. |
| Manual Base-16 Conversion + Simulation | O(n) | O(n) | Useful when demonstrating understanding of base conversion or when built-in utilities are restricted. |