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 <= 12
  • num does not contain leading zeros.
  • num represents an integer in the range [1, 1012].

Note: This problem is from LeetCode.
โŒ Compiled
โŒ Executed
โŒ Correct
program main
    implicit none

    character(len=12) :: num
    character(len=4) :: hexspeak

    ! Example 1
    num = "257"
    write(*,*) "Example 1:"
    write(*,*) "Input: ", trim(num)
    hexspeak = hexspeak_representation(num)
    write(*,*) "Output: ", trim(hexspeak)

    ! Example 2
    num = "3"
    write(*,*) "Example 2:"
    write(*,*) "Input: ", trim(num)
    hexspeak = hexspeak_representation(num)
    write(*,*) "Output: ", trim(hexspeak)

contains

    function hexspeak_representation(num) result(hexspeak)
        implicit none
        character(len=*), intent(in) :: num
        character(len=4) :: hexspeak
        character(len=12) :: hexstring

        hexstring = num
        call toupper(hexstring)
        hexspeak = ""
        do i = 1, len_trim(hexstring)
            select case (hexstring(i:i))
                case ("0")
                    hexspeak = hexspeak // "O"
                case ("1")
                    hexspeak = hexspeak // "I"
                case default
                    hexspeak = hexspeak // hexstring(i:i)
            end select
        end do

        if (all(hexspeak == ["A", "B", "C", "D", "E", "F", "I", "O"])) then
            hexspeak = trim(hexspeak)
        else
            hexspeak = "ERROR "
        end if

    end function hexspeak_representation

end program main
โŒ Compiled
โŒ Executed
โŒ Correct
PROGRAM Hexspeak

IMPLICIT NONE

CHARACTER(len=12) :: num
CHARACTER(len=12) :: hexspeak
INTEGER :: n

! Read input
READ(*,*) num

! Convert to integer
READ(num,*) n

! Convert to hexspeak
hexspeak = num
CALL replace_zero(hexspeak)
CALL replace_one(hexspeak)

! Check if valid
IF (hexspeak .EQ. "ERROR ") THEN
    WRITE(*,*) "ERROR "
ELSE
    WRITE(*,*) hexspeak
END IF

CONTAINS

SUBROUTINE replace_zero(str)
CHARACTER(len=12), INTENT(INOUT) :: str
INTEGER :: i

DO i = 1, 12
    IF (str(i:i) .EQ. "0") THEN
        str(i:i) = "O"
    END IF
END DO

END SUBROUTINE replace_zero

SUBROUTINE replace_one(str)
CHARACTER(len=12), INTENT(INOUT) :: str
INTEGER :: i

DO i = 1, 12
    IF (str(i:i) .EQ. "1") THEN
        str(i:i) = "I"
    END IF
END DO

END SUBROUTINE replace_one

END PROGRAM Hexspeak
๐ŸŒ Data from online sources
def to_hexspeak(num: str) -> str:
    n = int(num)
    hex_str = []
    while n:
        rem = n % 16
        if rem == 1: hex_str.insert(0, 'I')
        elif rem == 0: hex_str.insert(0, 'O')
        elif rem > 9: hex_str.insert(0, chr(rem - 10 + ord('A')))
        else: return 'ERROR'
        n //= 16
    return ''.join(hex_str)

The algorithm first converts the input number (given as a string) to a long integer (we'll call it n). We then use a loop to construct the Hexspeak representation in reverse order. In each iteration, we calculate the remainder rem of n divided by 16. If rem is 1, we append 'I' to the beginning of the string; if it is 0, we append 'O'; if it is greater than 9, we append the corresponding letter ('A' to 'F'); and if it is none of these, then the Hexspeak representation is invalid, and we return "ERROR". After processing each character, we update n by dividing it by 16. Finally, we return the constructed Hexspeak representation.

This algorithm runs in O(log(n)) time complexity, as each iteration of the loop reduces n by a factor of 16. The space complexity is also O(log(n)), as the constructed Hexspeak representation string has a length proportional to the number of hexadecimal digits in n.

๐ŸŒ Data from online sources
#include <string>

std::string toHexspeak(std::string num) {
    long n = std::stol(num);
    std::string hex = "";
    while (n) {
        int rem = n % 16;
        if (rem == 1) hex = "I" + hex;
        else if (rem == 0) hex = "O" + hex;
        else if (rem > 9) hex = (char)(rem - 10 + 'A') + hex;
        else return "ERROR";
        n /= 16;
    }
    return hex;
}

The algorithm first converts the input number (given as a string) to a long integer (we'll call it n). We then use a loop to construct the Hexspeak representation in reverse order. In each iteration, we calculate the remainder rem of n divided by 16. If rem is 1, we append 'I' to the beginning of the string; if it is 0, we append 'O'; if it is greater than 9, we append the corresponding letter ('A' to 'F'); and if it is none of these, then the Hexspeak representation is invalid, and we return "ERROR". After processing each character, we update n by dividing it by 16. Finally, we return the constructed Hexspeak representation.

This algorithm runs in O(log(n)) time complexity, as each iteration of the loop reduces n by a factor of 16. The space complexity is also O(log(n)), as the constructed Hexspeak representation string has a length proportional to the number of hexadecimal digits in n.