Roman to Integer - Problem

Roman numerals are an ancient numbering system still used today on clock faces, building dates, and movie credits. Your task is to decode these historical symbols and convert them back to modern integers!

The seven Roman numeral symbols and their values are:

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

Basic Addition Rule: Roman numerals are typically written from largest to smallest, left to right. For example: XII = 10 + 1 + 1 = 12

Subtraction Rule: However, there's a twist! To avoid writing four identical symbols in a row, Romans used a subtraction principle:

  • IV = 4 (5 - 1), not IIII
  • IX = 9 (10 - 1)
  • XL = 40 (50 - 10)
  • XC = 90 (100 - 10)
  • CD = 400 (500 - 100)
  • CM = 900 (1000 - 100)

Goal: Given a valid Roman numeral string, return its integer equivalent.

Input & Output

example_1.py โ€” Basic Roman Numeral
$ Input: s = "III"
โ€บ Output: 3
๐Ÿ’ก Note: III = 3. Simple addition of three I's: 1 + 1 + 1 = 3.
example_2.py โ€” Subtraction Case
$ Input: s = "IV"
โ€บ Output: 4
๐Ÿ’ก Note: IV = 4. This uses the subtraction rule: since I (1) comes before V (5), we subtract: 5 - 1 = 4.
example_3.py โ€” Complex Roman Numeral
$ Input: s = "MCMXC"
โ€บ Output: 1990
๐Ÿ’ก Note: MCMXC = 1990. Breaking it down: M (1000) + CM (900) + XC (90) = 1000 + 900 + 90 = 1990. Both CM and XC use subtraction rules.

Visualization

Tap to expand
Roman to Integer Conversion ProcessExample: Converting "XLIV" to 44X10L50I1V5Look aheadX (10) < L (50) โ†’ Subtract 10L (50) > I (1) โ†’ Add 50I (1) < V (5) โ†’ Subtract 1V (5) is last โ†’ Add 5Final Calculation:-10 + 50 - 1 + 5 = 44XL (40) + IV (4) = 44
Understanding the Visualization
1
Set Up Dictionary
Create a lookup table for each Roman symbol and its integer value
2
Read Left to Right
Process each character in the string from left to right
3
Look Ahead Rule
For each character, peek at the next character to determine add vs subtract
4
Apply Logic
If current < next: subtract current value. Otherwise: add current value
5
Sum Result
Continue until all characters are processed and return the final sum
Key Takeaway
๐ŸŽฏ Key Insight: The magic happens by looking ahead! When a smaller Roman numeral sits before a larger one, it signals subtraction. This one simple rule handles all special cases elegantly.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string, each character is processed exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a fixed-size hash map (7 entries) and a few variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 15
  • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M')
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999]
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 24 Apple 18
256.8K Views
Very High Frequency
~12 min Avg. Time
8.4K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen