Try the Question
You can try solving the problem here before reading the solution:
👉 https://leetcode.com/problems/roman-to-integer/
Practicing it yourself first helps you understand the logic better.
Roman to Integer – Complete Guide
Roman numerals are an ancient numbering system used by the Romans. Instead of digits like 1, 2, 3, they used specific characters to represent values.
This problem asks us to convert a Roman numeral string into its corresponding integer value.
It is a very common interview question and frequently appears in coding platforms like LeetCode, HackerRank, and coding interviews at product-based companies.
Roman Numeral Symbols
Roman numerals consist of seven characters, each representing a fixed value.
| Symbol | Value |
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
Example conversions:
- III = 3 → 1 + 1 + 1
- XII = 12 → 10 + 1 + 1
- XXVII = 27 → 10 + 10 + 5 + 1 + 1
Normally, Roman numerals are written from largest to smallest from left to right.
But there is an important exception.
The Subtraction Rule
In some cases, Roman numerals use subtraction instead of addition.
If a smaller value appears before a larger value, it means we subtract it.
Examples:
| Roman | Calculation | Result |
| IV | 5 − 1 | 4 |
| IX | 10 − 1 | 9 |
| XL | 50 − 10 | 40 |
| XC | 100 − 10 | 90 |
| CD | 500 − 100 | 400 |
| CM | 1000 − 100 | 900 |
These are the only valid subtraction cases.
Example Walkthrough
Example 1
Input
Explanation
Output
Example 2
Input
Explanation
Output
Example 3
Input
Explanation
Output
Intuition Behind the Solution
The key idea is to compare each Roman numeral with the numeral to its right.
Two situations occur:
Case 1 — Normal Addition
If the current value ≥ next value
We simply add it to the total.
Example
Case 2 — Subtraction Case
If the current value < next value
We subtract it.
Example
Strategy to Solve
- Create a HashMap to store Roman numeral values.
- Start from the rightmost character.
- Initialize total with the last character's value.
- Move from right to left.
- Compare the current numeral with the next numeral.
- If smaller → subtract.
- Otherwise → add.
- Continue until the beginning of the string.
This approach works because Roman numerals follow a local comparison rule.
Java Implementation
Code Explanation
Step 1 — Store Roman Values
We create a HashMap so we can quickly get the numeric value of each Roman character.
This allows O(1) lookup time.
Step 2 — Initialize Total
We start with the last character.
Why?
Because the last numeral is always added, never subtracted.
Step 3 — Traverse from Right to Left
We move backward through the string.
And compare with the previous character.
Step 4 — Apply Roman Rules
If the previous numeral is smaller than the current numeral:
Example
If it is greater or equal:
Example
Time Complexity
Where n = length of the string.
We only traverse the string once.
Space Complexity
The HashMap size is constant (only 7 entries).
Key Takeaways
✔ Roman numerals mostly use addition
✔ Subtraction occurs only in six specific cases
✔ Comparing current and next value is the simplest approach
✔ A single pass solution (O(n)) is enough
This is why this problem is commonly used to test string processing and logical reasoning in coding interviews.
Final Thoughts
The Roman to Integer problem is a great beginner-friendly problem that teaches:
- HashMap usage
- String traversal
- Conditional logic
- Pattern recognition
Mastering these simple problems builds a strong foundation for more complex algorithmic questions.
If you're preparing for coding interviews, this is definitely a problem you should practice.




