Problem
Ask
Submissions

Problem: Roman to Integer

Medium
30 min
Explore how to convert Roman numerals to integers by implementing hash maps and handling special subtraction cases. This lesson helps you understand the logic and constraints behind Roman numerals, enabling you to write efficient code that accurately converts any valid Roman numeral into its integer equivalent.

Statement

Given a string, s, representing a Roman numeral, return the integer value of the Roman numeral.

Seven different symbols represent Roman numerals:

Symbol

Value

I

1

V

5

X

10

L

50

C

100

D

500

M

1000

So, in Roman numerals, 22 is written as II\text {II}, which is simply two 11s added together. The number1212 is written as XII\text{XII}, which is simply X+II\text{X} + \text{II}. The number 2727 is written as XXVII\text{XXVII}, which breaks down into XX+V+II\text{XX} + \text{V} + \text{II}.

Roman numerals are usually written from largest to smallest from left to right (X\text{X}) comes first, and then I\text I for 1212). However, there are six cases where a smaller numeral is placed before a larger one to indicate subtraction. For example, the number 44 is written as IV\text{IV}, because the 11 comes before the 55, so we subtract it to get 44. The remaining such cases are as follows:

  1. IV\text{IV} (5511) = 44

  2. IX\text{IX} (101011) = 99

  3. XL\text{XL} (50501010) = 4040

  4. XC\text{XC} (1001001010) = 9090

  5. CD\text{CD} (500500100100) = 400400

  6. CM\text{CM} (10001000100100) = 900900

Constraints:

  • 11 \leq s.length 15\leq 15

  • s contains only the characters ‘I\text{I} ’, ‘V\text V ’, ‘X\text X’, ‘L\text L ’, ‘C\text C ’, ‘D\text D’, and ‘M\text M ’.

  • It is guaranteed that s is a valid Roman numeral in the range [11, 39993999].

Problem
Ask
Submissions

Problem: Roman to Integer

Medium
30 min
Explore how to convert Roman numerals to integers by implementing hash maps and handling special subtraction cases. This lesson helps you understand the logic and constraints behind Roman numerals, enabling you to write efficient code that accurately converts any valid Roman numeral into its integer equivalent.

Statement

Given a string, s, representing a Roman numeral, return the integer value of the Roman numeral.

Seven different symbols represent Roman numerals:

Symbol

Value

I

1

V

5

X

10

L

50

C

100

D

500

M

1000

So, in Roman numerals, 22 is written as II\text {II}, which is simply two 11s added together. The number1212 is written as XII\text{XII}, which is simply X+II\text{X} + \text{II}. The number 2727 is written as XXVII\text{XXVII}, which breaks down into XX+V+II\text{XX} + \text{V} + \text{II}.

Roman numerals are usually written from largest to smallest from left to right (X\text{X}) comes first, and then I\text I for 1212). However, there are six cases where a smaller numeral is placed before a larger one to indicate subtraction. For example, the number 44 is written as IV\text{IV}, because the 11 comes before the 55, so we subtract it to get 44. The remaining such cases are as follows:

  1. IV\text{IV} (5511) = 44

  2. IX\text{IX} (101011) = 99

  3. XL\text{XL} (50501010) = 4040

  4. XC\text{XC} (1001001010) = 9090

  5. CD\text{CD} (500500100100) = 400400

  6. CM\text{CM} (10001000100100) = 900900

Constraints:

  • 11 \leq s.length 15\leq 15

  • s contains only the characters ‘I\text{I} ’, ‘V\text V ’, ‘X\text X’, ‘L\text L ’, ‘C\text C ’, ‘D\text D’, and ‘M\text M ’.

  • It is guaranteed that s is a valid Roman numeral in the range [11, 39993999].