LeetCode Roman to Integer Solution Java Code Logic Explained
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:
Symbol | Value |
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
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

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

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

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

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!
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!