LeetCode roman to integer question
🏛️ 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!
Comments
Post a Comment