<AI/>
enru
All posts

Roman to integer

LeetCode 13: Convert a Roman numeral to an integer, handling the subtractive cases like IV and CM.

Difficulty
Easy
Published
Topics
#string#two pointers
Open on LeetCode

Problem Description

Write a function that takes a Roman numeral as a string and returns its integer value.

Constraints

  • The length of the Roman numeral string is in the range from 1 to 15
  • The string contains only valid uppercase Roman numerals

Examples

Input: III

Answer: 3

Explanation: III = I + I + I = 1 + 1 + 1 = 3

How Roman numerals are formed

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

For example, 2 is written as II in Roman numerals, which is just two ones. 12 is written as XII, which is simply X + II. 27 is written as XXVII, which is XX + V + II. Roman numerals are usually written from largest to smallest, left to right. However, 4 is not IIII. Instead, 4 is written as IV. Since the one comes before the five, we subtract it, getting four. The same principle applies to 9, which is written as IX.

There are six cases where subtraction is used:

  • I can be placed before V and X to make 4 and 9.
  • X can be placed before L and C to make 40 and 90.
  • C can be placed before D and M to make 400 and 900.

Solution

To solve the problem, we go through the string character by character, convert the Roman digits to integers and sum them up. The main difficulty is handling the subtraction rules (IV = 4, not 6, since the smaller digit I comes before the larger V).

To convert Roman digits, we create a hash map where the key is a Roman digit and the value is its integer value.

typescript
const runeToIntegerMap: Record<string, number> = {
  I: 1,
  V: 5,
  X: 10,
  L: 50,
  C: 100,
  D: 500,
  M: 1000,
};

For convenience, we also introduce another helper hash map that describes how pairs of Roman digits turn into a number in the subtractive cases.

typescript
const runeToIntegerDecrementsMap: Record<string, Record<string, number>> = {
  I: {
    V: 4,
    X: 9,
  },
  X: {
    L: 40,
    C: 90,
  },
  C: {
    D: 400,
    M: 900,
  },
};

By looking at the current and the next digit with runeToIntegerDecrementsMap, we can easily get the correct number.

With these two helper maps, all that's left is to go through the string character by character, convert each Roman digit (checking not only the current but also the next character) to a number and add it to the result:

typescript
const runeToIntegerMap: Record<string, number> = {
  I: 1,
  V: 5,
  X: 10,
  L: 50,
  C: 100,
  D: 500,
  M: 1000,
};
 
const runeToIntegerDecrementsMap: Record<string, Record<string, number>> = {
  I: {
    V: 4,
    X: 9,
  },
  X: {
    L: 40,
    C: 90,
  },
  C: {
    D: 400,
    M: 900,
  },
};
 
export const romanToInt = (s: string): number => {
  let res = 0;
 
  for (let i = 0; i < s.length; i++) {
    if (i < s.length - 1) {
      const possibleDecrements = runeToIntegerDecrementsMap[s[i]];
 
      if (possibleDecrements) {
        const realNum = possibleDecrements[s[i + 1]];
 
        if (realNum) {
          res += realNum;
          i++;
          continue;
        }
      }
    }
 
    res += runeToIntegerMap[s[i]];
  }
 
  return res;
};

Complexity Analysis

Time Complexity

To convert the number we iterate over the whole string of length n. There are no other operations that depend on the string length, so the time complexity is O(n).

Space Complexity

O(1), since the algorithm doesn't allocate memory that depends on the string length n.