Skip to content
Decoder

Roman to Integer

Easy

Problem

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).

Example 1

Input
s = "III"
Output
3

Example 2

Input
s = "LVIII"
Output
58
Why
L + V + III

Example 3

Input
s = "MCMXCIV"
Output
1994
Why
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].

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.