Ask to Arya

LeetCode Roman to Integer Solution Java Code Logic Explained

Jun 9, 2025, 9:39 AM

When I first looked at LeetCode Problem 13, Roman to Integer, I was excited. It felt familiar — I knew the symbols, but I wasn’t sure how to translate them into a working Java function. In this post, I’ll walk you through exactly how I solved it, the mistakes I made, what I learned, and the final strategy that worked.

Problem Statement (Quick Recap)

We are given a Roman numeral as a string (like "XII" or "MCMXCIV") and we need to convert it into an integer. Roman numerals are made of these symbols:

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

Sponsored content

Ad loading... (may be blocked)

There’s a trick: sometimes the value is formed by subtraction. For example:

  • IV = 5 - 1 = 4
  • IX = 10 - 1 = 9
  • CM = 1000 - 100 = 900

My Strategy

my Strategy
my Strategy

1. Use a HashMap for Fast Lookups

The first idea that hit me was to use a HashMap to store all the Roman symbols and their corresponding integer values. Why? Because HashMap.get() is fast (O(1)), and we need to look up symbol values repeatedly.

HashMap<Character, Integer> map = new HashMap<>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);

Sponsored content

Ad loading... (may be blocked)

2. Prepare to Return a Number

Since this is a function, I knew I had to return a number. So I initialized an integer variable:

int num = 0;

3. Convert the String into a Char Array

Sponsored content

Ad loading... (may be blocked)

I decided to convert the input string s into a character array using:

char[] arr = s.toCharArray();

Why? It made it easier to access each symbol using indices.

Also, I calculated the length once and stored it in a variable len. That saved me from calling s.length multiple times, which is good practice.

Sponsored content

Ad loading... (may be blocked)

4. First Attempt: Just Add Values

Sponsored content

Ad loading... (may be blocked)

My first approach was simple: loop through each character and add its value from the map to num.

This worked for inputs like "III" (which gives 3) or "VIII" (which gives 8).

Sponsored content

Ad loading... (may be blocked)

5. Bug Discovered: Subtraction Rule

Bug discovered
Bug discovered

But then I tested "IV".

Sponsored content

Ad loading... (may be blocked)

Expected: 4

Got: 6 😅

I realized I was missing the subtraction logic. Roman numerals sometimes have a smaller value before a larger one — which means we subtract.

Sponsored content

Ad loading... (may be blocked)

6. Fix: Add a Special Condition

fix the condition
fix the condition

Sponsored content

Ad loading... (may be blocked)

So I added a condition inside the loop:

  • If the current symbol is smaller than the next one, and they are not the same, then subtract the current value from the next and skip the next character.

Here’s the key condition I used:

if (i ! = len - 1 && arr[i] ! = arr[i + 1] && map.get(arr[i + 1]) > map.get(arr[i]))

When this condition is true:

val = map.get(arr[i + 1]) - map.get(arr[i]);
i += 1; // Skip the next symbol

Sponsored content

Ad loading... (may be blocked)

Else, just do normal addition.

Sponsored content

Ad loading... (may be blocked)

Final Code

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

        int num = 0;
        char[] arr = s.toCharArray();
        int len = arr.length;

        for (int i = 0; i < len; i++) {
            int val = 0;
            if (i ! = len - 1 && arr[i] ! = arr[i + 1] && map.get(arr[i + 1]) > map.get(arr[i])) {
                val = map.get(arr[i + 1]) - map.get(arr[i]);
                i++;
            } else {
                val = map.get(arr[i]);
            }
            num += val;
        }

        return num;
    }
}
Final Output
Final Output

Sponsored content

Ad loading... (may be blocked)

What I Learned

  • HashMaps are great when you need fast lookups (especially for symbol-value mappings).
  • Careful condition checks are necessary for special cases like Roman numeral subtraction.
  • Breaking the problem into small steps makes debugging easier.
  • Mistakes are part of learning — every bug helped me understand Roman numerals more deeply!

Sponsored content

Ad loading... (may be blocked)

Final Thoughts

This was a great exercise in logic and coding. It reminded me that even “easy” problems can have tricky parts, and it’s okay to stumble a bit. As long as you keep thinking and debugging, you’ll get there!

Tags: #leetcode problem 13#roman number to number#dsa problem#my strategy on the problem