Roman to Integer: LeetCode Java Solution

Aman WalkeAman Walke
3 min read

๐Ÿ‘‹ Introduction

Roman numerals were once the universal language of counting โ€” from ancient Rome to modern programming puzzles. In this post, weโ€™ll dive into LeetCode Problem #13: Roman to Integer, decode the logic, and implement a clean Java solution.

๐Ÿงฉ Problem Statement

Given a Roman numeral, convert it to an integer.

Roman numerals are represented by seven symbols:

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

But thereโ€™s a twist:

Some numbers use subtractive notation:

  • IV = 4 โ†’ 5 - 1

  • IX = 9 โ†’ 10 - 1

  • XL = 40 โ†’ 50 - 10

  • XC = 90 โ†’ 100 - 10

  • CD = 400 โ†’ 500 - 100

  • CM = 900 โ†’ 1000 - 100

๐Ÿ” Examples

Input: "III"
Output: 3

Input: "LVIII"
Output: 58
Explanation: L = 50, V = 5, III = 3

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90, IV = 4

๐Ÿง  Approach: Look Ahead and Subtract if Needed

๐ŸŽฏ Idea:

  • Start from the left of the Roman numeral.

  • If the current symbol is smaller than the next symbol, subtract its value.

  • Otherwise, add it.

๐Ÿ—บ๏ธ Strategy:

  1. Use a Map<Character, Integer> to store Roman symbol values.

  2. Traverse the string, comparing each character with the one after it.

  3. Apply subtraction rule where applicable.

๐Ÿ’ป Java Code

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

        int result = 0;
        int n = s.length();

        for (int i = 0; i < n; i++) {
            int current = romanMap.get(s.charAt(i));

            // Check if there's a next character and it's larger
            if (i + 1 < n && current < romanMap.get(s.charAt(i + 1))) {
                result -= current; // Subtract if smaller than the next
            } else {
                result += current;
            }
        }

        return result;
    }
}

๐Ÿงฎ Time and Space Complexity

ComplexityValue
โฑ๏ธ TimeO(n) โ€” one pass through the string
๐Ÿง  SpaceO(1) โ€” fixed map size (7 symbols)

โœ… Summary

This problem is a great exercise in applying domain rules (Roman numeral logic) in code. Key takeaways:

  • Use a Map for clean symbol-value lookups.

  • Detect subtractive combinations by comparing adjacent values.

  • One pass and done โ€” efficient and elegant!

0
Subscribe to my newsletter

Read articles from Aman Walke directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aman Walke
Aman Walke

I'm a tech enthusiast who loves building backend systems that just work โ€” clean, scalable, and efficient. I've worked with microservices, Spring Boot, Azure, and APIs, and I enjoy digging into root causes and making systems better. Whether it's writing clean code, reviewing it, or managing deployments with DevOps tools, I'm always up for the challenge. I like working in collaborative environments where I can learn, share, and grow alongside smart people.