Coding Interview Questions | Roman to Integer

January 20th, 2024

Roman to Integer Problem Introduction:

This summary will talk about my solution to the roman to integer problem as seen on leetcode here. A synopsis of the problem summary will be shown below:

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

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, 2 is written as II in Roman numeral, just two ones added together.
12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII.
Instead, the number four is written as IV. Because the one is before the five we subtract it making four.
The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

How do you solve the Roman to Integer Problem in O(n) time?

Solution:

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

const subtractionMapping: Map<string, Set<string>> = new Map();
subtractionMapping.set("I", new Set(["V", "X"]));
subtractionMapping.set("X", new Set(["L", "C"]));
subtractionMapping.set("C", new Set(["D", "M"]));

function romanToInt(s: string): number {
  let sIdx = 0;
  let total = 0;

  while (sIdx < s.length) {
    const currentRoman = s[sIdx];
    const subtractionRoman = subtractionMapping.get(currentRoman);

    // Character may be able to perform subtraction
    if (subtractionRoman && sIdx + 1 < s.length) {
      const nextRoman = s[sIdx + 1];
      if (subtractionRoman.has(nextRoman)) {
        total += romanMapping[nextRoman] - romanMapping[currentRoman];
        sIdx += 2;
        continue;
      }
    }

    total += romanMapping[currentRoman];
    sIdx++;
  }

  return total;
}

Roman to Integer Solution Summary:

Below is a breakdown of the key aspects of the solution above for the Roman to Integer conversion problem:

  1. Roman to Integer Mapping: The solution utilizes a constant romanMapping which is a record mapping Roman numerals to their corresponding integer values. This mapping is crucial for converting each Roman numeral to its numerical equivalent.

  2. Subtraction Mapping: To handle subtraction cases where a smaller numeral precedes a larger one, a subtractionMapping is employed. It is a Map associating each subtractive Roman numeral with a set of Roman numerals that can be subtracted after it.

  3. Iterative Approach: The algorithm employs a while loop to iterate through the input string s. It processes each Roman numeral character and calculates the corresponding integer value.

  4. Subtraction Handling: The solution checks for cases where subtraction is applicable. If the current Roman numeral can be subtracted, it adjusts the total accordingly by subtracting the smaller numeral from the larger one.

  5. Total Calculation: The total is continually updated by adding the integer value of each Roman numeral to it during the iteration.

Complexities

  1. Time Complexity: The time complexity of the solution is linear, denoted as O(n), where n is the length of the input string s. The algorithm processes each character once.

  2. Space Complexity: The space complexity is constant, denoted as O(1), as the additional space used (such as the mapping constants) does not depend on the size of the input.