LeetCode: Converting Roman Numerals to Integers

Hero image for LeetCode: Converting Roman Numerals to Integers. Image by Anne Nygård.
Hero image for 'LeetCode: Converting Roman Numerals to Integers.' Image by Anne Nygård.

I've recently written about converting Western Integers into Roman Numerals using TypeScript, so it only makes sense that I put together a quick partner article for doing the conversion in reverse: Roman to Integer is a function which accepts Roman Numerals and outputs the correct corresponding integer value.


A Quick Overview

I go into this in more detail in my previous article, but to offer a quick overview of the techniques that define how Roman Numerics work: Roman numerals consist of seven symbols: I, V, X, L, C, D, and M. These translate to numerical values like this:

  • I: 1
  • V: 5
  • X: 10
  • L: 50
  • C: 100
  • D: 500
  • M: 1000

Roman Numerals are formatted from largest to smallest value (lefttoright), but if you encounter a smaller value before a larger one (e.g., IV), then you subtract the smaller value from the larger (in this case: IV = 4). Here are those edge cases:

  • IV: 4 (5 1)
  • IX: 9 (10 1)
  • XL: 40 (50 10)
  • XC: 90 (100 10)
  • CD: 400 (500 100)
  • CM: 900 (1000 100)

The Challenge: Decoding Roman Numerals

Much like encoding Romanal Numerics from integers, the solution to converting from Roman Numerals to integers lies in understanding the order and values of Roman numerals. In a systematised solution, we simply have to follow the same rules by traversing the Roman numeral string and deciding whether to add the current numeral's value or subtract it (in the case of the subtractive notations).

The Code

Using clientside development (TypeScript in this instance), we can achieve this relatively simply:

const romanToInt = (s: string): number => {  const roman: { [key: string]: number } = {    I: 1,    V: 5,    X: 10,    L: 50,    C: 100,    D: 500,    M: 1000,  };  let total = 0;  let prevValue = 0;  for (let i = s.length - 1; i >= 0; i--) {    const currentValue = roman[s[i]];    if (currentValue < prevValue) {      total -= currentValue;    } else {      total += currentValue;    }    prevValue = currentValue;  }  return total;};

How It Works

  1. Roman Dictionary:

    We firstly set up a dictionary to convert Roman numerals to their respective integer values.
  2. Iterate Through String:

    Starting at the end of the string, we decipher each numeral.
  3. Compare with Previous Value:

    By checking the previous numeral's value, we decide whether to add or subtract the current numeral's value.

Final Thoughts

As I said previously, the likelihood of you coming across a realworld usecase for this type of functionality is relatively rare. However, the Roman numeral system offers an interesting study into cultural differences when it comes to fundamental concepts like numbers.

Understanding the way that the two compare and how to decode and convert between one and the other is a development challenge that helps reinforce algorithmic thinking and problemsolving; invaluable skills in any web developer! Whether you're decoding an ancient script or a new data format, the core skills remain the same.


Categories:

  1. Algorithms
  2. Development
  3. Guides
  4. JavaScript
  5. LeetCode