Skip to content
Onsite

Roman to Integer

EasyP2Coding

Convert the Roman numeral s to an integer. The symbols are I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000. Normally symbols are written from largest to smallest and added, but a smaller symbol in front of a larger one is subtracted (IV = 4, IX = 9, XL = 40, XC = 90, CD = 400, CM = 900).

s is a valid numeral for a number from 1 to 3999.

Example 1

Input
s = "III"
Output
3

Example 2

Input
s = "LVIII"
Output
58

Example 3

Input
s = "MCMXCIV"
Output
1994

Constraints

  • 1 <= s.length <= 15
  • s is a valid Roman numeral in the range [1, 3999].

Hints

Run checks the 3 visible tests, Submit all 12.