ExamAdda Logo

Roman to Integer

Easy

Roman numerals are represented by seven different symbols:

I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000

Given a Roman numeral string s, convert it to an integer.

Roman numerals are usually written from largest to smallest from left to right. However, in the following cases, subtraction is used:

• I can be placed before V and X to make 4 and 9.
• X can be placed before L and C to make 40 and 90.
• C can be placed before D and M to make 400 and 900.

Return the integer value of the Roman numeral.

Example 1

Input

III

Output

3

Explanation

III = 1 + 1 + 1 = 3.

Example 2

Input

LVIII

Output

58

Explanation

L = 50, V = 5, III = 3.

Therefore, LVIII = 50 + 5 + 3 = 58.

Constraints

1 ≤ s.length ≤ 15
s contains only the characters 'I'
'V'
'X'
'L'
'C'
'D'
and 'M'.
It is guaranteed that s is a valid Roman numeral.

Hints:

Hint 1

Assign an integer value to each Roman numeral symbol.

Hint 2

Traverse the string from left to right. If the current symbol has a smaller value than the next symbol, subtract it; otherwise, add it.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 4, 2026

Last updated

Aug 4, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output