Skip to content
CodeShare
All problems

Roman to Integer

Convert a Roman numeral into a number.

EasyStringsMathHash map

Roman numerals use seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000.

Symbols are usually written from largest to smallest and added together. When a smaller symbol comes right before a larger one, it is subtracted instead: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400 and CM = 900.

Given a valid Roman numeral s, return its value.

Examples

Example 1
Input
s = "III"
Output
3
Example 2
Input
s = "LVIII"
Output
58
Why
L = 50, V = 5, III = 3.
Example 3
Input
s = "MCMXCIV"
Output
1994
Why
M = 1000, CM = 900, XC = 90, IV = 4.

Constraints

  • 1 <= s.length <= 15
  • s is a valid Roman numeral between 1 and 3999.