Last Updated: March 28, 2026
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.
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.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.
result = 0.result and remove it from the string.result.result.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?
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.
By subtracting the smaller value and then adding the larger value on the next iteration, we get the correct combined result. For IV: we subtract 1, then add 5, giving us 4. For CM: we subtract 100, then add 1000, giving us 900. The validity guarantee from the constraints means we never need to worry about invalid sequences like IC.
result = 0.0 to n - 1.i, get the current character's value.i < n - 1 and the current value is less than the next character's value, subtract it from result.result.result.result, n, and current.