Roman to Integer
The problem
Roman numerals are represented by seven different symbols:
- I = 1
- V = 5
- X = 10
- L = 50
- C = 100
- D = 500
- M = 1000
Roman numerals are typically written from largest to smallest, left to right. However, in specific cases, a smaller numeral placed before a larger one indicates subtraction.
The following subtractive combinations are valid:
- I before V (5) and X (10) → 4 and 9
- X before L (50) and C (100) → 40 and 90
- C before D (500) and M (1000) → 400 and 900
Given a Roman numeral, convert it to an integer.
**Input: **s = "III" **Output: **3 **Explanation: **III = 1 + 1 + 1 = 3
**Input: **s = "XLII" **Output: **42 **Explanation: **XL = 40, II = 2 → 40 + 2 = 42
**Input: **s = "DCCCXC"
- 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]
cpp
class Solution {
public:
int romanToInt(string s) {
// Your code goes here
}
};java
class Solution {
public int romanToInt(String s) {
// Your code goes here
}
}python
class Solution:
def romanToInt(self, s: str) -> int:
# Your code goes herejavascript
class Solution {
romanToInt(s) {
// Your code goes here
}
}csharp
public class Solution
{
public int RomanToInt(string s)
{
// Your code goes here
}
}go
func romanToInt(s string) int {
// Your code goes here
}Stuck? Show a way to structure it+
- 01Map each Roman symbol to its value.
- 02Scan from right to left.
- 03Track the largest value seen to the right.
- 04Subtract a smaller symbol before a larger one.
- 05Otherwise add it and update the maximum.
Reference answer
Then expect these follow-ups
How would you convert an integer to Roman numerals?
Tests: follow-up reasoning
How would you validate canonical Roman notation?
Tests: follow-up reasoning
Hiring now
Software engineer roles are open right now
landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Senior Software Engineer -.NET
Tyler Technologies · Annapolis, Maryland

Software Development Engineer II
SaaS Labs · Bengaluru, Karnataka, India

Advanced Software Engineer - Product Architect (Legal, Safety, and Risk)
Metropolitan Transportation Authority · New York, NY
Next question