543. Hexspeak

A decimal number can be converted to its Hexspeak representation by:

  • Converting it to an uppercase hexadecimal string.
  • Replacing all occurrences of '0' with 'O' and '1' with 'I'.
  • Ensuring that the resulting string contains only the characters from the set {'A', 'B', 'C', 'D', 'E', 'F', 'I', 'O'}.

If the final string contains any other digits (2-9), return "ERROR"

Example 1:

Input: num = "256"

Output: "IOO"

Explanation:

256 in hexadecimal is "100".

Replace '1' → 'I', '0' → 'O', so it becomes "IOO".

Since all characters are valid, return "IOO".

Example 2:

Input: num = "4095"

Output: "FFF"

Explanation:

4095 in hexadecimal is "FFF".

All characters are already in Hexspeak, so return "FFF".

Now Your Turn!

Pick the correct output for the given input

Input: num = "1234"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
string toHexspeak(string num) {
// Your code goes here
}
};
 
Test Case

Input:

N