Skip to main content
Back to problems
#1271
Easy Algorithms

Hexspeak

Math String
58.3% acceptance
Mar 31, 2026
80
127

No description available.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn to_hexspeak(num: String) -> String {
    let n: u64 = num.parse().unwrap();
    let hex = format!("{:X}", n);
    let mut result = String::new();
    for c in hex.chars() {
      match c {
        '0' => result.push('O'),
        '1' => result.push('I'),
        'A'..='F' => result.push(c),
        _ => return "ERROR".to_string(),
      }
    }
    result
  }
}