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

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

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);

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

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.

4. First Attempt: Just Add Values

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).

5. Bug Discovered: Subtraction Rule

Bug discovered
Bug discovered

But then I tested "IV".

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.

6. Fix: Add a Special Condition

fix the condition
fix the condition

So I added a condition inside the loop:

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

Else, just do normal addition.

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

What I Learned

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