Roman to Integer

Easy

Given a roman numeral, convert it to an integer.

Roman numerals are usually written largest to smallest from left to right, but six combinations use subtraction:

  • I before V (5) or X (10) → 4, 9
  • X before L (50) or C (100) → 40, 90
  • C before D (500) or M (1000) → 400, 900

Example 1

romanToInt()
Input
s = "III"
Output
3

Example 2

romanToInt()
Input
s = "MCMXCIV"
Output
1994

Why

M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints
1 <= s.length <= 15
s contains only the characters 'I', 'V', 'X', 'L', 'C', 'D', 'M'.

Topics

StringsHash Table

Implement romanToInt in the editor and return the answer — printing it is never graded.