Convert a valid Roman numeral to its integer value. The symbols are I=1, V=5, X=10, L=50, C=100, D=500, M=1000. A smaller symbol placed immediately before a larger one is subtracted (for example IV=4, IX=9, XL=40); otherwise symbol values are added left to right.
One line: a valid Roman numeral for an integer between 1 and 3999 (uppercase letters).
One line: the integer value.
Example 1
Input
MCMXCIV
Expected
1994
Explanation
M=1000, CM=900, XC=90, IV=4, total 1994.
Example 2
Input
LVIII
Expected
58
Explanation
L=50, V=5, III=3, total 58.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →