Practice Roman to Integer
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Convert a valid Roman numeral into its integer value. Symbol values are I=1, V=5, X=10, L=50, C=100, D=500, and M=1000. Usually values are added. The subtractive pairs IV, IX, XL, XC, CD, and CM instead represent 4, 9, 40, 90, 400, and 900. Practice approach: Scan left to right. Subtract a symbol when the following symbol has a larger value; otherwise add it. The input is guaranteed valid, so this covers the allowed subtractive pairs. Runtime is O(n) with constant extra storage.
Source: Roman to Integer on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
s = "III"
3
# Example 2
s = "LVIII"
58
# Example 3
s = "MCMXCIV"
1994
Constraints
How to solve Roman to Integer
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
In a valid numeral a symbol is subtractive exactly when the symbol immediately to its right is larger, so the entire conversion is one comparison per character against its right neighbor, with no table of pairs needed.
Why brute force is not enough
Match the string from the left against a table of all thirteen tokens (seven symbols plus six subtractive pairs), always taking the longest match. With s.length at most 15 there is no speed problem; the cost is extra code and substring comparisons that the neighbor rule makes unnecessary.
Approach
- Map each of the seven symbols to its value: I to 1, V to 5, X to 10, L to 50, C to 100, D to 500, M to 1000.
- Scan from left to right. For each symbol, look up its value and the value of the next symbol, treating the position past the end as zero.
- If the current value is strictly less than the next value, subtract it from the running total; otherwise add it. The invariant is that the total equals the numeric value of the prefix processed so far.
- Return the total after the final symbol, which is always added since nothing follows it.
Walkthrough of Example 3
s = "MCMXCIV" → 1994
- M (1000) is followed by C (100), which is smaller, so add: total 1000.
- C (100) is followed by M (1000), which is larger, so subtract: total 900. Then M (1000) is followed by X (10), add: total 1900.
- X (10) is followed by C (100), subtract: total 1890. Then C (100) is followed by I (1), add: total 1990.
- I (1) is followed by V (5), subtract: total 1989. V (5) is last, add: total 1994.
Time and space
Time O(n) · Space O(1). Each of the n characters (at most 15) is examined once with a constant-time lookup; the symbol table has a fixed seven entries.
Interview pitfalls
- Reading past the end of the string when peeking at the next symbol on the last index; guard it or treat the end as value zero.
- Using less-than-or-equal instead of strictly less when deciding to subtract, which turns repeated symbols like III into subtractions.
- Handling two-character tokens first (IV, IX, and so on) but forgetting to advance the index by two, counting the second character again.
- Mixing up L (50) and D (500), or C (100) and D, in the lookup table under time pressure.
Follow-up questions interviewers ask
- How would you convert an integer back to a Roman numeral, and why does a greedy largest-first approach work there?
- How would you validate a numeral, including the rule that a symbol repeats at most three times and only specific pairs are subtractive?
- How would you extend to values above 3999 using the overline notation?
Further reading on the technique: Roman numerals
Related LeetCode Problems
Practice more problems similar to Roman to Integer.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.