Back to LeetCode Series

LeetCode #13: Roman to Integer

Convert a Roman numeral string to an integer by scanning left to right — and watching for subtractive cases.

Difficulty: EasyPattern: String, MappingTime: 6 min read

🏛️ Roman Numerals Recap

Roman numerals use letters to represent values, with special cases for subtraction:

Basic Symbols:
  • I = 1
  • V = 5
  • X = 10
  • L = 50
  • C = 100
  • D = 500
  • M = 1000
Subtractive Cases:
  • IV = 4, IX = 9
  • XL = 40, XC = 90
  • CD = 400, CM = 900

The key: if a smaller value appears before a larger one, it's subtracted.

The Problem

Given a valid Roman numeral string (from I to MMMCMXCIX), convert it to an integer.

Examples

Input: "III" → Output: 3  (1 + 1 + 1)

Input: "IV" → Output: 4  (5 - 1)

Input: "IX" → Output: 9  (10 - 1)

Input: "LVIII" → Output: 58  (50 + 5 + 1 + 1 + 1)

Input: "MCMXCIV" → Output: 1994  (1000 + 900 + 90 + 4)

✅ Algorithm: Left-to-Right Scan

We scan each character and:

  1. Look up the value of the current symbol
  2. If the current value is less than the next value, subtract it
  3. Otherwise, add it
  4. Move to the next symbol
Example: "MCMXCIV"
  • M (1000) → next is C (100) → 1000 > 100 → +1000
  • C (100) → next is M (1000) → 100 < 1000 → -100
  • M (1000) → no next → +1000
  • X (10) → next is C (100) → 10 < 100 → -10
  • C (100) → next is I (1) → 100 > 1 → +100
  • I (1) → next is V (5) → 1 < 5 → -1
  • V (5) → no next → +5
Total: 1000 - 100 + 1000 - 10 + 100 - 1 + 5 = 1994

Solution (JavaScript)

function romanToInt(s) {
  const map = {
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000
  };

  let total = 0;

  for (let i = 0; i < s.length; i++) {
    const current = map[s[i]];
    const next = map[s[i + 1]] || 0;

    if (current < next) {
      total -= current;
    } else {
      total += current;
    }
  }

  return total;
}

🧠 Why This Works

Because Roman numerals are written in descending order — except for subtractive cases.

Whenever a smaller value appears before a larger one, it means subtraction. This algorithm catches that by comparing each symbol to the next.

🎮 Try It: Step-by-Step Decoder

Watch how each symbol is added or subtracted based on the next one.

🌍 Real-World Applications

Roman numeral parsing is used in real systems — from media to legacy software.

🎬 Film & TV Metadata

When scraping movie titles like "Star Wars: Episode IV", systems convert "IV" to 4 for sorting.

📚 Book & Document Processing

OCR and PDF parsers convert Roman page numbers (e.g., "xvii") to integers for indexing.

🧩 Puzzles & Games

Escape rooms, trivia apps, and educational games often include Roman numeral challenges.

💾 Legacy Systems

Some older databases and file formats use Roman numerals in IDs or versioning.

💡 Fun Fact: The Romans didn’t use subtractive notation consistently — "IIII" was often used for 4 on clocks! Our modern rules were standardized later.

Summary

This problem teaches how to parse structured strings using simple rules — a skill used in compilers, configuration parsers, and data validation.

View All Challenges