AlgoMaster Logo

Roman to Integer

Last Updated: March 28, 2026

easy

Understanding the Problem

We're given a string of Roman numeral characters and need to return the integer it represents. The tricky part isn't the basic mapping of symbols to values. That's straightforward. The real challenge is handling the subtraction cases: when a smaller value appears before a larger one, we subtract instead of add.

Take MCMXCIV. Reading left to right, most characters just add their value. But CM means 900 (1000 - 100), XC means 90 (100 - 10), and IV means 4 (5 - 1). So the key question is: how do we know when to add and when to subtract?

The observation that makes this clean is simple. If the current character's value is smaller than the next character's value, subtract it. Otherwise, add it. That one rule handles every case, including all six subtraction patterns.

Key Constraints:

  • 1 <= s.length <= 15 → The string is extremely short (at most 15 characters). Even an O(n^2) approach would finish instantly. There's no performance concern here at all.
  • s contains only valid Roman numeral characters → We don't need to handle invalid input or unknown characters.
  • s is a valid Roman numeral in range [1, 3999] → We won't encounter malformed sequences. The input always follows proper Roman numeral rules.

Approach 1: Replace Subtraction Pairs Then Sum

Intuition

The most beginner-friendly way to think about this: before trying to handle the subtraction logic during iteration, just get rid of the subtraction cases entirely. Replace every two-character subtraction pair (IV, IX, XL, XC, CD, CM) with its combined value first, then sum up what's left.

The idea is simple. Scan the string for all six subtraction pairs, add their combined value to the result, and remove them from the string. Then iterate over whatever characters remain and add their individual values. This separates the "hard part" (subtraction) from the "easy part" (addition) into two distinct phases.

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 approach works but does multiple passes over the string and creates new string objects during replacement.

What if we could determine whether to add or subtract each character in a single left-to-right pass?

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

Intuition

Here's the clean insight that makes this problem elegant. In a valid Roman numeral, when a smaller-valued symbol appears before a larger-valued symbol, it means subtraction. In every other case, it means addition.

So the algorithm is: walk through the string left to right. At each position, check if the current symbol's value is less than the next symbol's value. If it is, subtract the current value. If it isn't, add it. That's it. One pass, no string manipulation, no special cases to enumerate.

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