Skip to content
Bill Liao
Go back

Roman to Integer

Edit page

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:

Given a roman numeral, convert it to an integer.

Example 1:

Input: s = “III” Output: 3 Explanation: III = 3.

Example 2:

Input: s = “LVIII” Output: 58 Explanation: L = 50, V= 5, III = 3.

Example 3:

Input: s = “MCMXCIV” Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Constraints:

Approach: Left-to-Right Scan with Subtraction Rule

Algorithm

  1. Map each Roman symbol to its value
  2. Iterate through the string from left to right
  3. If the current symbol is smaller than the next symbol, subtract its value (e.g., IV = 4)
  4. Otherwise, add its value
  5. Return the total

Time & Space Complexity

Java Implementation

import java.util.HashMap;
import java.util.Map;

public class RomanToInteger {

    private static final Map<Character, Integer> VALUES = new HashMap<>() {{
        put('I', 1);
        put('V', 5);
        put('X', 10);
        put('L', 50);
        put('C', 100);
        put('D', 500);
        put('M', 1000);
    }};

    /**
     * Convert a roman numeral to an integer.
     * @param s Roman numeral string
     * @return Integer value
     */
    public static int romanToInt(String s) {
        int total = 0;

        for (int i = 0; i < s.length(); i++) {
            int current = VALUES.get(s.charAt(i));

            if (i + 1 < s.length() && current < VALUES.get(s.charAt(i + 1))) {
                total -= current;
            } else {
                total += current;
            }
        }

        return total;
    }

    // Test method
    public static void main(String[] args) {
        System.out.println("Input: s = \"III\"");
        System.out.println("Output: " + romanToInt("III")); // Expected: 3

        System.out.println("Input: s = \"LVIII\"");
        System.out.println("Output: " + romanToInt("LVIII")); // Expected: 58

        System.out.println("Input: s = \"MCMXCIV\"");
        System.out.println("Output: " + romanToInt("MCMXCIV")); // Expected: 1994
    }
}

Example Walkthrough

For s = "MCMXCIV":

  1. M: 1000 < next C? No. total=1000
  2. C: 100 < next M (1000)? Yes. total=1000-100=900
  3. M: 1000 < next X? No. total=1900
  4. X: 10 < next C (100)? Yes. total=1900-10=1890
  5. C: 100 < next I? No. total=1990
  6. I: 1 < next V (5)? Yes. total=1990-1=1989
  7. V: 5, last. total=1994

Key Points

  1. Subtraction Rule: A smaller symbol before a larger one means subtraction
  2. Guaranteed Valid Input: No need to validate the roman numeral format
  3. Single Pass: Compare each symbol with the next to decide add or subtract
  4. O(1) Space: A fixed lookup table is used
  5. Range: Valid values are always within [1, 3999]

Edit page
Share this post:

Previous Post
Pow(x, n)
Next Post
Find First and Last Position of Element in Sorted Array