Roman to Integer
Roman to Integer is LeetCode 13 (Easy). You are given a Roman numeral s and must return its value. The seven symbols are I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000.
- Adding: symbols are normally written from largest to smallest, and their values add up:
XIIis 12. - Subtracting: six pairs put a smaller symbol before a larger one to mean the difference:
IV4,IX9,XL40,XC90,CD400,CM900.
The input is always a valid numeral between 1 and 3999, at most 15 symbols long.
- 1 <= s.length <= 15
- s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
- It is guaranteed that s is a valid roman numeral in the range [1, 3999].
Intuition
You do not need to search for the six pairs. They all have the same shape: a smaller symbol directly before a larger one. So compare each symbol with the one after it:
- Smaller than the next: it is the front of a pair, so subtract it.
- Otherwise: add it. The last symbol has nothing after it and is always added.
Subtracting the front and then adding the larger symbol gives the pair's value: for IX that is −1 + 10 = 9.
When a symbol's meaning depends only on its neighbour, one left-to-right pass that looks at s[i + 1] replaces a list of special cases. Here the neighbour decides whether a symbol counts as plus or minus.
Approach
Before reading on, write MCMXCIV as a sum of seven signed numbers, one per symbol, and check that it adds up to 1994. Aim for O(n) time.
Map symbols to values
Store the seven values in a dictionary (a switch works too). The input is guaranteed to be a valid numeral, so there is nothing to validate.
Add or subtract each symbol
Walk the string from left to right. If there is a next symbol and the current value is strictly smaller than it, subtract the current value; otherwise add it.
Code in Python, C++ and Java
IV or CM, so it is subtracted; every other symbol is added.I equals the next symbol, and equal symbols simply add up. That is why the test is <, not <=: add 1.I equals the next symbol, and equal symbols simply add up. That is why the test is <, not <=: add 1.I is the last symbol. Nothing follows it, so it can never be subtracted: add 1.IV or CM, so it is subtracted; every other symbol is added.L (50) is larger than the next symbol V (5), the normal largest-first order: add 50.V (5) is larger than the next symbol I (1), the normal largest-first order: add 5.I equals the next symbol, and equal symbols simply add up. That is why the test is <, not <=: add 1.I equals the next symbol, and equal symbols simply add up. That is why the test is <, not <=: add 1.I is the last symbol. Nothing follows it, so it can never be subtracted: add 1.IV or CM, so it is subtracted; every other symbol is added.M (1000) is larger than the next symbol C (100), the normal largest-first order: add 1000.C (100) is smaller than the next symbol M (1000), so together they form CM = 900. Subtract 100 now; adding 1000 next gives exactly 900.M (1000) is larger than the next symbol X (10), the normal largest-first order: add 1000.X (10) is smaller than the next symbol C (100), so together they form XC = 90. Subtract 10 now; adding 100 next gives exactly 90.C (100) is larger than the next symbol I (1), the normal largest-first order: add 100.I (1) is smaller than the next symbol V (5), so together they form IV = 4. Subtract 1 now; adding 5 next gives exactly 4.V is the last symbol. Nothing follows it, so it can never be subtracted: add 5.Common pitfalls
Reading past the last symbol
if value[s[i]] < value[s[i + 1]]:
if i + 1 < len(s) and value[s[i]] < value[s[i + 1]]:
For the last symbol s[i + 1] does not exist, so Python raises IndexError and C++ reads past the string. The last symbol is never subtracted, so check that a next symbol exists first.
Using <= instead of <
if value[s[i]] <= value[s[i + 1]]:
if value[s[i]] < value[s[i + 1]]:
Equal symbols add: III is 3. With <= the first two Is are subtracted and III comes out as −1 − 1 + 1 = −1.
Complexity
Roman to Integer FAQ
Can it be solved from right to left?
Yes. Walk backwards and remember the value of the symbol to the right. Add the current symbol if it is at least that value, subtract it if it is smaller. It makes the same decisions as the left-to-right pass, but never looks ahead, so there is no end-of-string check.
What is the difference between Roman to Integer and Integer to Roman?
Roman to Integer (LeetCode 13) reads a numeral and returns its value, so the six pairs are found by comparing neighbours. Integer to Roman (LeetCode 12) goes the other way: it puts the pairs into the value table (900 = CM, 400 = CD, ...) and greedily takes the largest value that still fits.