๐๏ธ Roman to Integer โ Mastering a Classic Coding Problem

Difficulty: Easy
LeetCode Problem #13
Whether you're prepping for coding interviews or brushing up your algorithm skills, converting Roman numerals to integers is a classic problem worth mastering. Let's walk through the logic, break it down step-by-step, and solve it using PHP, Python, Java, and JavaScript.
๐ What Are Roman Numerals?
Roman numerals are made up of seven symbols:
Symbol | Value |
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Generally, symbols are placed from largest to smallest from left to right. But there are six special subtraction cases:
I before V or X โ 4 (
IV
), 9 (IX
)X before L or C โ 40 (
XL
), 90 (XC
)C before D or M โ 400 (
CD
), 900 (CM
)
๐ง Problem Statement
Given a Roman numeral as a string, convert it to an integer.
๐ Strategy
Create a mapping of Roman symbols to their values.
Loop through the string:
Compare current symbol with the next one.
If current >= next โ add it.
If current < next โ subtract it.
Sum everything and return the result.
๐ Example: "MCMXCIV"
M(1000) + CM(900) + XC(90) + IV(4) = 1994
Detailed Steps:
Symbol | Current | Next | Action | Running Total |
M | 1000 | C | Add 1000 | 1000 |
C | 100 | M | Subtract 100 | 900 |
M | 1000 | X | Add 1000 | 1900 |
X | 10 | C | Subtract 10 | 1890 |
C | 100 | I | Add 100 | 1990 |
I | 1 | V | Subtract 1 | 1989 |
V | 5 | - | Add 5 | 1994 |
๐งโ๐ป PHP Code
class Solution {
function romanToInt($s) {
$roman = [
'I'=> 1, 'V'=> 5, 'X'=>10,
'L'=>50, 'C'=>100, 'D'=>500, 'M'=>1000
];
$length = strlen($s);
$total = 0;
for ($i = 0; $i < $length; $i++) {
$current = $roman[$s[$i]];
$next = $roman[$s[$i + 1]] ?? 0;
if ($current >= $next) {
$total += $current;
} else {
$total -= $current;
}
}
return $total;
}
}
๐ Python Code
class Solution:
def romanToInt(self, s: str) -> int:
roman = {
'I': 1, 'V': 5, 'X': 10,
'L': 50, 'C': 100, 'D': 500, 'M': 1000
}
total = 0
for i in range(len(s)):
current = roman[s[i]]
next_val = roman[s[i + 1]] if i + 1 < len(s) else 0
if current >= next_val:
total += current
else:
total -= current
return total
โ Java Code
class Solution {
public int romanToInt(String s) {
Map<Character, Integer> roman = Map.of(
'I', 1, 'V', 5, 'X', 10,
'L', 50, 'C', 100, 'D', 500, 'M', 1000
);
int total = 0;
for (int i = 0; i < s.length(); i++) {
int current = roman.get(s.charAt(i));
int next = (i + 1 < s.length()) ? roman.get(s.charAt(i + 1)) : 0;
if (current >= next) {
total += current;
} else {
total -= current;
}
}
return total;
}
}
๐ JavaScript Code
var romanToInt = function(s) {
const roman = {
'I': 1, 'V': 5, 'X': 10,
'L': 50, 'C': 100, 'D': 500, 'M': 1000
};
let total = 0;
for (let i = 0; i < s.length; i++) {
let current = roman[s[i]];
let next = roman[s[i + 1]] || 0;
if (current >= next) {
total += current;
} else {
total -= current;
}
}
return total;
};
โ Edge Cases
Input | Output | Notes |
"III" | 3 | 1 + 1 + 1 |
"IV" | 4 | 5 - 1 |
"IX" | 9 | 10 - 1 |
"LVIII" | 58 | 50 + 5 + 3 |
"MCMXCIV" | 1994 | Detailed above |
๐ Complexity
Time: O(n), where n is the length of the string
Space: O(1), constant space for the map
๐ฏ Final Thoughts
The Roman to Integer problem is not just a string parsing exercise โ it teaches you:
The power of hash maps
Simple greedy algorithms
Conditional logic in iterative loops
Understanding this will make future parsing or conversion problems easier to grasp. Add it to your toolkit of interview essentials!
Subscribe to my newsletter
Read articles from Nature directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
