Roman to Integer

Easy

Topics
Hash TableMathString

Given a Roman numeral string s, convert it to an integer. Symbols: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. A smaller symbol before a larger one is subtracted (e.g., IV = 4).

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].
Run ⌘' · Submit ⌘⏎