Roman to Integer: The Subtraction Rule Pattern
Roman to Integer (#13) is one of the most-solved Easy problems on LeetCode, and for good reason. It is a clean introduction to hash map lookups combined with simple conditional logic. If you are just starting your roman to integer leetcode journey, this problem is the perfect first step.
The core idea is straightforward. Roman numerals use seven symbols — I, V, X, L, C, D, M — each with a fixed integer value. Most of the time you simply add them up left to right. The only twist is the subtraction rule: when a smaller value appears immediately before a larger one, you subtract instead of adding. That single rule handles every tricky case like IV (4), IX (9), XL (40), XC (90), CD (400), and CM (900).
This problem is a great warm-up for any coding interview session because it tests two fundamental skills: mapping symbols to values with a hash map, and making conditional decisions as you iterate through a string.
Understanding the Problem
You are given a string representing a Roman numeral and need to convert it to an integer. The input is guaranteed to be a valid Roman numeral in the range 1 to 3999. Roman to Integer (#13) is one of the top 10 most-solved Easy problems on LeetCode, making it a popular choice for warm-up practice.
The seven Roman numeral symbols and their values are: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000. In most cases, symbols are written from largest to smallest, left to right. For example, "XII" is simply 10 + 1 + 1 = 12.
The subtraction cases are the key to solving this problem. When a smaller value appears before a larger value, it represents subtraction: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400, CM = 900. There are exactly six subtraction combinations you need to handle, and they all follow the same pattern.
- Input: a string of Roman numeral characters (I, V, X, L, C, D, M)
- Output: the corresponding integer value
- Range: 1 to 3999 (valid Roman numerals only)
- Six subtraction cases: IV=4, IX=9, XL=40, XC=90, CD=400, CM=900
Interview Favorite
Roman to Integer (#13) is one of the top 10 most-solved Easy problems on LeetCode — it's a popular warm-up question that tests hash map usage and conditional logic.
The Subtraction Rule: One Simple Check
The subtraction rule for roman numeral conversion boils down to a single comparison. As you iterate through the string from left to right, compare the current symbol value to the next symbol value. If the current value is less than the next value, subtract it from the total. Otherwise, add it. That is the entire algorithm.
This works because every subtraction case in Roman numerals involves exactly one smaller symbol placed before one larger symbol. IV means "subtract 1 from 5," XC means "subtract 10 from 100," and so on. You never see two consecutive subtraction symbols — the grammar of Roman numerals guarantees this.
The time complexity is O(n) where n is the length of the string, since you make a single pass. The space complexity is O(1) because the hash map has a fixed size of seven entries regardless of input length. This is as efficient as it gets for this problem.
Implementation: Hash Map and Loop
The implementation of roman to int is remarkably clean. First, create a hash map that maps each Roman numeral character to its integer value. Then, iterate through the string. At each position, look up the current value and the next value. If current is less than next, subtract current from the total. Otherwise, add it.
Here is the approach in plain English. Initialize a result variable to 0. For each character in the string, get its value from the hash map. If this value is less than the value of the next character (and there is a next character), subtract it from result. Otherwise, add it to result. Return result when done.
The leetcode 13 solution requires no sorting, no recursion, and no complex data structures. It is a single for-loop with one if-else branch. This simplicity is what makes it such a popular interview warm-up — it tests whether you can translate a clear rule into clean code without overcomplicating things.
- Create a hash map: {I:1, V:5, X:10, L:50, C:100, D:500, M:1000}
- Initialize result = 0
- Loop through the string: if value[i] < value[i+1], subtract value[i]; else add value[i]
- Return result after the loop completes
The One-Line Rule
The subtraction rule in one line: if the current symbol's value is less than the next symbol's value, subtract it instead of adding. This handles IV, IX, XL, XC, CD, CM in the same logic.
Visual Walkthrough: MCMXCIV Equals 1994
Let us walk through the roman to integer explained process with a concrete example. The input is "MCMXCIV" and the expected output is 1994. This example contains multiple subtraction cases, making it ideal for understanding the pattern.
Start with result = 0. At index 0, M = 1000. The next character is C = 100. Since 1000 is not less than 100, add 1000. Result = 1000. At index 1, C = 100. The next character is M = 1000. Since 100 < 1000, subtract 100. Result = 900. At index 2, M = 1000. The next character is X = 10. Since 1000 is not less than 10, add 1000. Result = 1900.
Continuing: at index 3, X = 10. The next character is C = 100. Since 10 < 100, subtract 10. Result = 1890. At index 4, C = 100. The next character is I = 1. Since 100 is not less than 1, add 100. Result = 1990. At index 5, I = 1. The next character is V = 5. Since 1 < 5, subtract 1. Result = 1989. At index 6, V = 5. No next character, so add 5. Result = 1994.
Each step is a simple lookup and comparison. The subtraction rule naturally handles CM (900), XC (90), and IV (4) without any special-case code. One rule covers all six subtraction combinations.
Edge Cases to Consider
Even though this problem is straightforward, there are a few edge cases worth considering when you convert roman numeral strings. A single character like "I" or "M" should simply return its value — there is no next character to compare against, so you just add it.
Repeated characters like "III" (3) or "XXX" (30) have no subtraction involved. Each character value is greater than or equal to the next, so you add every time. The longest valid Roman numeral in the constraint range is "MMMCMXCIX" which equals 3999. It combines both addition and subtraction cases and is a good comprehensive test.
A purely subtractive input like "IV" (4) contains only one subtraction pair. Your loop should handle this correctly: subtract 1 at index 0, add 5 at index 1, yielding 4. If your solution gives 6 for "IV" instead of 4, you likely have the comparison direction reversed.
- Single character: "I" = 1, "M" = 1000 — just add, no comparison needed
- All same: "III" = 3, "XXX" = 30 — pure addition, no subtraction triggered
- Longest valid: "MMMCMXCIX" = 3999 — tests both rules together
- Subtractive only: "IV" = 4 — verify subtraction logic works in isolation
Direction Matters
Process left to right, not right to left — while right-to-left works too, left-to-right with the 'look ahead' comparison is more intuitive and what interviewers expect.
What Roman to Integer Teaches You
Roman to Integer is more than just an easy problem to check off your list. It teaches two transferable skills that show up in harder problems. First, it demonstrates the power of hash map lookups for symbol-to-value translation — a pattern that appears in problems like Two Sum (#1), Valid Anagram (#242), and Group Anagrams (#49).
Second, it introduces the "look ahead" comparison technique. Deciding what to do with the current element based on the next element is a pattern that recurs in monotonic stacks, greedy algorithms, and string parsing problems. Roman to Integer is the gentlest possible introduction to this idea.
If you are preparing for coding interviews, use YeetCode flashcards to drill hash map patterns and conditional iteration. Roman to Integer is an excellent starting point, but the real value comes from recognizing when the same underlying techniques apply to medium and hard problems. Master the fundamentals here, and they will carry you through dozens of harder challenges.