Roman to Integer – Easy Explanation with Java Solution (LeetCode 13)
Roman to Integer – Easy Explanation with Java Solution (LeetCode 13)

Roman to Integer – Easy Explanation with Java Solution (LeetCode 13)

Learn how to convert Roman numerals into integers using a simple algorithm. This guide explains the intuition, rules of Roman numerals, and a step-by-step Java solution for the popular LeetCode problem.

14 views
0
0

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.

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

Example conversions:

  1. III = 3 → 1 + 1 + 1
  2. XII = 12 → 10 + 1 + 1
  3. 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:

RomanCalculationResult
IV5 − 14
IX10 − 19
XL50 − 1040
XC100 − 1090
CD500 − 100400
CM1000 − 100900

These are the only valid subtraction cases.


Example Walkthrough

Example 1

Input

s = "III"

Explanation

I + I + I = 1 + 1 + 1 = 3

Output

3

Example 2

Input

s = "LVIII"

Explanation

L = 50
V = 5
III = 3

50 + 5 + 3 = 58

Output

58

Example 3

Input

s = "MCMXCIV"

Explanation

M = 1000
CM = 900
XC = 90
IV = 4

Total = 1994

Output

1994


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

VI
5 + 1 = 6


Case 2 — Subtraction Case

If the current value < next value

We subtract it.

Example

IV
5 - 1 = 4


Strategy to Solve

  1. Create a HashMap to store Roman numeral values.
  2. Start from the rightmost character.
  3. Initialize total with the last character's value.
  4. Move from right to left.
  5. Compare the current numeral with the next numeral.
  6. If smaller → subtract.
  7. Otherwise → add.
  8. Continue until the beginning of the string.

This approach works because Roman numerals follow a local comparison rule.


Java Implementation

class Solution {
public int romanToInt(String s) {
HashMap<Character,Integer> mp = new HashMap<>();
mp.put('I',1);
mp.put('V',5);
mp.put('X',10);
mp.put('L',50);
mp.put('C',100);
mp.put('D',500);
mp.put('M',1000);

int tot = mp.get(s.charAt(s.length()-1));
char arr[] = s.toCharArray();
int j = s.length()-2;

for(int i = arr.length-1; i >= 0; i--){
if(j >= 0){
if(mp.get(arr[j]) < mp.get(arr[i])){
tot -= mp.get(arr[j]);
}else if(mp.get(arr[j]) > mp.get(arr[i])){
tot += mp.get(arr[j]);
}else{
tot += mp.get(arr[j]);
}
}
j--;
}
return tot;
}
}


Code Explanation


Step 1 — Store Roman Values

We create a HashMap so we can quickly get the numeric value of each Roman character.

I → 1
V → 5
X → 10
...

This allows O(1) lookup time.


Step 2 — Initialize Total

We start with the last character.

int tot = mp.get(s.charAt(s.length()-1));

Why?

Because the last numeral is always added, never subtracted.


Step 3 — Traverse from Right to Left

We move backward through the string.

for(int i = arr.length-1; i >= 0; i--)

And compare with the previous character.


Step 4 — Apply Roman Rules

If the previous numeral is smaller than the current numeral:

tot -= value

Example

IV
5 - 1

If it is greater or equal:

tot += value

Example

VI
5 + 1


Time Complexity

O(n)

Where n = length of the string.

We only traverse the string once.


Space Complexity

O(1)

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:

  1. HashMap usage
  2. String traversal
  3. Conditional logic
  4. 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.

Ai Assistant Kas