AlgoMaster Logo

Roman to Integer

easyFrequency5 min readUpdated June 23, 2026

Understanding the Problem

We're given a string of Roman numeral characters and need to return the integer it represents. Mapping each symbol to its value is direct. The work is in the subtraction cases: when a smaller value appears before a larger one, those two characters combine into a single subtracted amount.

Take MCMXCIV. Reading left to right, most characters add their value. But CM means 900 (1000 - 100), XC means 90 (100 - 10), and IV means 4 (5 - 1). The question is how to decide, at each character, whether to add or subtract.

One comparison settles it. If a character's value is smaller than the value of the character to its right, subtract it. Otherwise, add it. That rule covers all six subtraction patterns and every additive character.

Key Constraints:

  • 1 <= s.length <= 15 → The string is at most 15 characters, so even repeated scans of it are negligible. The approach choice here is about clarity, not speed.
  • s is a valid Roman numeral in range [1, 3999] → The input always follows proper Roman numeral rules, so there are no invalid or unknown characters to guard against, and no malformed sequences like IIII or IC. The largest possible result, 3999, fits comfortably in a 32-bit integer, so there is no overflow risk.

Approach 1: Replace Subtraction Pairs Then Sum

Intuition

One way to sidestep the add-or-subtract decision is to remove the subtraction cases before doing any addition. Each of the six subtraction pairs (IV, IX, XL, XC, CD, CM) stands for a fixed value, so we can find each pair, add its value, and delete it from the string. After that, every remaining character is purely additive, and we sum them with a plain lookup.

This relies on a property of the [1, 3999] range: each subtraction pair appears at most once in any valid numeral. For example, a numeral can contain IV only once, since two fours would never be written that way. That is why removing a single occurrence of each pair is enough.

Algorithm

  1. Initialize result = 0.
  2. For each of the six subtraction pairs (CM, CD, XC, XL, IX, IV), check if it exists in the string. If it does, add its combined value to result and remove it from the string.
  3. Iterate through the remaining characters. For each one, look up its value and add it to result.
  4. Return result.

Example Walkthrough

1Initial string: s = "MCMXCIV", result = 0
0
M
1
C
2
M
3
X
4
C
5
I
6
V
1/8

Code

This makes several passes over the string and allocates a new string on each replacement. A single left-to-right pass can make the add-or-subtract decision per character without any of that.

Approach 2: Left-to-Right with Lookahead (Optimal)

Intuition

In a valid Roman numeral, a smaller-valued symbol placed before a larger-valued symbol means subtraction. In every other position, a symbol adds its value. That distinction can be read off a single comparison between a character and the one to its right.

The algorithm walks the string left to right. At each position, it compares the current symbol's value to the next symbol's value. If the current value is smaller, it subtracts the current value; otherwise it adds it. This is one pass with no string manipulation and no need to enumerate the subtraction pairs.

Algorithm

  1. Create a map from each Roman numeral character to its integer value.
  2. Initialize result = 0.
  3. Iterate through the string from index 0 to n - 1.
  4. At each index i, get the current character's value.
  5. If i < n - 1 and the current value is less than the next character's value, subtract it from result.
  6. Otherwise, add it to result.
  7. Return result.

Example Walkthrough

1Initialize: i=0, result = 0
0
M
i
1
C
2
M
3
X
4
C
5
I
6
V
1/8

Code