Sitemap

LeetCode 13: Roman to Integer — Java Solution Explained

3 min readOct 22, 2026

--

Difficulty: Easy
Topics: String, Simulation

1. Understanding the Problem

In this problem, we are given a string containing a Roman numeral, and our task is to convert it into an integer.

Roman numerals use seven symbols:

Press enter or click to view image in full size
https://leetcode.com/problems/roman-to-integer/description/

Usually, Roman numerals are written from largest to smallest, and we add their values.

For example:

  • III = 1 + 1 + 1 = 3
  • LVIII = 50 + 5 + 1 + 1 + 1 = 58
  • XXVII = 10 + 10 + 5 + 1 + 1 = 27

However, there is one important exception: sometimes we need to subtract instead of adding.

2. Why Do We Need to Subtract?

Consider the Roman numeral IV.

If we simply add the values, we get:

1 + 5 = 6

But the correct answer is 4!

Why? Because when a smaller value appears before a larger value in the six permitted subtraction combinations, the smaller value must be subtracted from the larger one.

For example:

  • IV = 5 − 1 = 4
  • IX = 10 − 1 = 9
  • XL = 50 − 10 = 40
  • XC = 100 − 10 = 90
  • CD = 500 − 100 = 400
  • CM = 1000 − 100 = 900

Therefore, we cannot simply add every character’s value. We must compare the current character with the next character.

Get Priyanka’s stories in your inbox

Join Medium for free to get updates from this writer.

Our approach:

  • If the current value is smaller than the next value, subtract it.
  • Otherwise, add it.

This simple observation is the key to solving the problem.

3. Approach

We can solve this problem by traversing the string from left to right.

Step 1: Create a helper method getInt() that returns the integer value of a Roman numeral character.

Step 2: Initialize result = 0.

Step 3: Iterate through the string and get the current character’s value.

Step 4: Check whether a next character exists. If the current value is smaller than the next value, subtract the current value from result. Otherwise, add it.

Step 5: Return the final result.

The condition i + 1 < s.length() ensures that we do not access a character outside the string.

4. Java Solution

class Solution {
public static int getInt(char ch) {
switch (ch) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}
public int romanToInt(String s) {
int result = 0;
for (int i = 0; i < s.length(); i++) {
int current = getInt(s.charAt(i));
if (i + 1 < s.length()
&& current < getInt(s.charAt(i + 1))) {
result -= current;
} else {
result += current;
}
}
return result;
}
}

5. Complexity Analysis

Time Complexity: O(n)

We traverse the string once. Each iteration performs a constant amount of work, so the time complexity is linear in the length of the string.

Space Complexity: O(1)

We use only a few integer variables. The additional space remains constant regardless of the input length.

7. Key Takeaway

The most important part of this problem is understanding when to add and when to subtract.

Instead of checking every possible Roman numeral combination separately, we can compare each character with the next character.

Remember:

  • Current value smaller than next value → subtract.
  • Current value greater than or equal to next value → add.

This approach gives us a simple, efficient solution that runs in O(n) time and uses O(1) extra space.

Happy coding!

--

--

Priyanka
Priyanka

Written by Priyanka

Software developer sharing practical insights on development & modern technology. Writing about concepts, lessons learned & real-world development experiences.