Home
DSA Patterns
Hash Map
Roman to Integer
Roman to Integer (LeetCode 13)
Roman to Integer (LeetCode 13): convert a Roman numeral to an integer in one pass with a symbol-value map: subtract a symbol that is smaller than the next one, otherwise add it.
Problem Understanding
Roman to Integer: convert a valid Roman numeral (1 to 3999) to an integer. Symbols are I 1, V 5, X 10, L 50, C 100, D 500, M 1000. They are written largest to smallest, except in six subtraction pairs: IV 4, IX 9, XL 40, XC 90, CD 400, CM 900.
Example: "MCMXCIV" returns 1994 (M = 1000, CM = 900, XC = 90, IV = 4).
The Intuition: Look One Symbol Ahead
Store the seven symbol values in a hash map (or a switch) for O(1) lookups. Then one rule handles every case:
- If the current symbol is smaller than the next one, it is the first half of a subtraction pair: subtract it.
- Otherwise add it.
IV is -1 + 5 = 4; VI is 5 + 1 = 6. There is no need to list the six pairs. Like Two Sum, the map turns each step into a constant-time lookup.
Roman to Integer Visualization
total = 0
s (value below each symbol)
1000
100
1000
10
100
1
5
value
:
total
0
Answer
:
total = 0. Look each symbol up and compare it with the one after it
Next
M = 1000, not smaller than the next C = 100: add it. total = 1000
s
Each symbol shows its value underneath. The current symbol is filled and the next one is marked, since that comparison decides the step. Subtracted symbols turn orange and added ones green. Try Every pair for a numeral built entirely from subtraction pairs.
Roman to Integer Dry Run
Input: s = "MCMXCIV"
| Symbol | Value | Next | Action | Total |
|---|---|---|---|---|
| M | 1000 | C (100) | add | 1000 |
| C | 100 | M (1000) | subtract | 900 |
| M | 1000 | X (10) | add | 1900 |
| X | 10 | C (100) | subtract | 1890 |
| C | 100 | I (1) | add | 1990 |
| I | 1 | V (5) | subtract | 1989 |
| V | 5 | - | add | 1994 |
Answer: 1994.
Roman to Integer LeetCode Solution
A value map and a one-symbol look-ahead. C uses a switch instead of a map.
Roman to Integer Code
1var romanToInt = function(s) {2 const value = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 };3 let total = 0;4 for (let i = 0; i < s.length; i++) {5 const v = value[s[i]];6 if (i + 1 < s.length && v < value[s[i + 1]]) total -= v;7 else total += v;8 }9 return total;10};
Roman to Integer in Python, Java, C++, TypeScript and C
Roman to Integer in Python
class Solution:
def romanToInt(self, s: str) -> int:
value = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
total = 0
for i, c in enumerate(s):
if i + 1 < len(s) and value[c] < value[s[i + 1]]:
total -= value[c]
else:
total += value[c]
return totalRoman to Integer in Java
class Solution {
public int romanToInt(String s) {
Map<Character, Integer> value = 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 v = value.get(s.charAt(i));
if (i + 1 < s.length() && v < value.get(s.charAt(i + 1))) total -= v;
else total += v;
}
return total;
}
}Roman to Integer in C++
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> value = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
int total = 0;
for (int i = 0; i < (int)s.size(); i++) {
int v = value[s[i]];
if (i + 1 < (int)s.size() && v < value[s[i + 1]]) total -= v;
else total += v;
}
return total;
}
};Roman to Integer in TypeScript
function romanToInt(s: string): number {
const value: Record<string, number> = { 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++) {
const v = value[s[i]];
if (i + 1 < s.length && v < value[s[i + 1]]) total -= v;
else total += v;
}
return total;
}Roman to Integer in C
int value(char c) {
switch (c) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
default: return 1000;
}
}
int romanToInt(char* s) {
int total = 0;
for (int i = 0; s[i]; i++) {
int v = value(s[i]);
if (s[i + 1] && v < value(s[i + 1])) total -= v;
else total += v;
}
return total;
}Edge Cases & Common Mistakes
- Last symbol: it has no next symbol, so it is always added; guard the look-ahead with
i + 1 < n. - Equal neighbours (
II,XX): equal is not smaller, so both are added. - Subtracting the pair twice: handle only the smaller symbol with the look-ahead; the larger one is then added normally.
- Single symbol:
"M"is 1000; the loop adds it. - Integer to Roman: the reverse problem, Integer to Roman (LeetCode 12), walks the same value table greedily from 1000 down to 1.
Summary
Map each symbol to its value. Walk the string once: subtract a symbol that is smaller than the one after it, add every other symbol. O(n) time and O(1) space.
More practice: DSA interview questions with answers · patterns cheat sheet
