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:
| Symbol | Value |
|---|---|
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
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), notIIIIIX = 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
Visualization
Time & Space Complexity
Single pass through the string, each character is processed exactly once
Only using a fixed-size hash map (7 entries) and a few variables
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]