Menu
CoddyTech

Roman to Integer

Roman numerals use seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. The symbols are written from largest to smallest and added up, except in six subtractive pairs where a smaller symbol comes first and is taken away from the larger one: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400 and CM = 900.

You get a valid Roman numeral s. Return the integer it stands for.

Function

romanToInt(s: string) → integer
sstring
a valid Roman numeral in capital letters
Returnsinteger
the value of the numeral, from 1 to 3999

Constraints

  • 1 ≤ s.length ≤ 15
  • s contains only the characters I, V, X, L, C, D and M.
  • s is a valid Roman numeral for a value from 1 to 3999.

Examples

Input
s = "XXVII"
Output
27
Explanation
XX is 10 + 10, V is 5 and II is 1 + 1, so the total is 27. No symbol is followed by a larger one, so every symbol is added.

lock icon+22 hidden tests on Submit

challenge icon

Follow-up

Can you write the reverse, turning an integer from 1 to 3999 into its Roman numeral?

Reset code
def romanToInt(s):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

s = "XXVII"

Expected

27