Skip to content

Roman to Integer

EasyBit Manipulation & Math
Decode it first. Clues, constraints, pattern and plan before any code.

Convert a Roman numeral to an integer. The symbols are I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000. Symbols are usually written largest to smallest and added up, but a smaller symbol placed before a larger one is subtracted (IV is 4, IX is 9, XL is 40, CM is 900).

s = "III"      →  3
s = "LVIII"    →  58     (L + V + III)
s = "MCMXCIV"  →  1994   (M + CM + XC + IV)

Constraints

  • 1 <= s.length <= 15
  • s contains only the characters I, V, X, L, C, D, M.
  • s is a valid Roman numeral in the range [1, 3999].