Description

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Given a roman numeral, convert it to an integer. Return the result as an integer.

Examples

Input:s = "III"
Output:3
Explanation:

Each 'I' represents 1. Three I's in sequence add up: I(1) + I(1) + I(1) = 3.

Input:s = "LVIII"
Output:58
Explanation:

L = 50, V = 5, and III = 3. Since each symbol is >= the one after it, simply add: 50 + 5 + 3 = 58.

Input:s = "MCMXCIV"
Output:1994
Explanation:

M=1000. CM means C(100) before M, so 1000-100=900. XC means X(10) before C, so 100-10=90. IV means I before V, so 5-1=4. Total: 1994.

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].

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!