Skip to main content

Command Palette

Search for a command to run...

Roman to Integer: LeetCode Java Solution

Published
•3 min read•View as Markdown
Roman to Integer: LeetCode Java Solution
A

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.

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

LeetCode Solutions in Java

Part 1 of 6

A series of clear, well-explained LeetCode solutions in Java. Each post covers the problem approach, Java code, and complexity analysis to help you strengthen your data structures and algorithms skills—one problem at a time.

Up next

Palindrome Number - LeetCode Java Solution

👋 Introduction When we think about palindromes, we usually think about words like "madam" or "racecar", which read the same forward and backward. But numbers can be palindromes too! In this blog post, we’ll walk through LeetCode Problem 9: Palindrom...