EasyPythonJavaScriptTypeScript
Strings — Roman Numeral to Integer
AlgorithmsStringsHash Map
solve(s) — convert a valid Roman numeral (1..3999) to an integer. The symbols areI=1 V=5 X=10 L=50 C=100 D=500 M=1000. Symbols are normally written largest to smallest and
added, except in six subtractive cases (IV=4 IX=9 XL=40 XC=90 CD=400 CM=900) where a
smaller symbol before a larger one is subtracted.
Scan left to right: when a symbol is smaller than the one after it, subtract it,
otherwise add it.
Examples
solve("III") → 3
solve("LVIII") → 58
solve("MCMXCIV") → 1994Sample tests
Test #1Repeated symbol
Input: ["III"]
Output: 3
Test #2Additive numeral
Input: ["LVIII"]
Output: 58
Test #3Several subtractive pairs
Input: ["MCMXCIV"]
Output: 1994