dsa · easy

Roman to Integer

Roman numerals use the symbols I=1, V=5, X=10, L=50, C=100, D=500, M=1000. They are usually written largest to smallest from the left. A smaller symbol immediately before a larger one is subtracted: IV=4, IX=9, XL=40, XC=90, CD=400, CM=900.

Arguments

Given a valid Roman string s, return its integer value.

Example

s = "MCMXCIV" → 1000 + (1000-100) + (100-10) + (5-1) = 1994.

Constraints

1 <= s.length <= 15 s contains only I, V, X, L, C, D, M and is a valid numeral. Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
"III"

Expected:
3

Example 2

Input:
"LVIII"

Expected:
58

Example 3

Input:
"MCMXCIV"

Expected:
1994

Open in the Dojo editor