Skip to content

Practice Roman to Integer

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#13
LeetCode Problem

Roman to Integer

Problem Statement

Convert a valid Roman numeral into its integer value. Symbol values are I=1, V=5, X=10, L=50, C=100, D=500, and M=1000. Usually values are added. The subtractive pairs IV, IX, XL, XC, CD, and CM instead represent 4, 9, 40, 90, 400, and 900. Practice approach: Scan left to right. Subtract a symbol when the following symbol has a larger value; otherwise add it. The input is guaranteed valid, so this covers the allowed subtractive pairs. Runtime is O(n) with constant extra storage.

Source: Roman to Integer on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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 contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
It is guaranteed that s is a valid roman numeral in the range [1, 3999].

Related LeetCode Problems

Practice more problems similar to Roman to Integer.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.