Roman to Integer
The drill: Convert a Roman numeral string back into the integer it represents — normally symbol values just add up left to right, except when a smaller symbol sits directly before a larger one, which signals subtraction instead.
A Roman numeral arrives as a string built from the usual symbols — I, V, X, L, C, D, M — and the task is to convert it back into the plain integer it represents.
Most of the time, symbol values simply add together left to right. The exception is a smaller symbol sitting directly in front of a larger one, like IV or CM, which signals that the smaller value should be subtracted rather than added.
Every input is a numeral that could legitimately appear on a clock face or a page number — a valid Roman representation of some integer — so the conversion never has to guess at malformed symbol sequences.
- numerals correspond to integers from 1 up to a few thousand
- the string uses only the standard symbols I, V, X, L, C, D, M
- at most one smaller symbol precedes a given larger one at any point
- every input is a well-formed Roman numeral
HINT 1 THE NUDGE
Summing every symbol's plain value overshoots exactly at the subtractive pairs — IV, IX, XL, XC, CD, CM — where a smaller symbol borrows meaning from the larger one right after it.
HINT 2 THE STRUCTURE
Each of those pairs, once summed naively, is off by precisely twice the smaller symbol's value — because it got added when it should have been subtracted.
HINT 3 ONE STEP FROM THE ANSWER
Either sum every symbol and then subtract 2× the smaller value at every place a symbol is immediately followed by a larger one, or walk left to right comparing each symbol only to its neighbor: add it normally, but subtract it when the next symbol outranks it.
X=10, I=1, V=5. Walk left to right, comparing each symbol only to its immediate neighbor.
class Solution:
def romanToInt(self, s: str) -> int:
values = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
total = 0
for i, c in enumerate(s):
v = values[c]
if i + 1 < len(s) and v < values[s[i + 1]]:
total -= v
else:
total += v
return totalclass Solution:
def romanToInt(self, s: str) -> int:
values = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
total = sum(values[c] for c in s)
for i in range(len(s) - 1):
if values[s[i]] < values[s[i + 1]]:
total -= 2 * values[s[i]] # it was added once above; remove it twice to subtract
return totalclass Solution {
public int romanToInt(String s) {
Map<Character, Integer> values = new HashMap<>();
values.put('I', 1);
values.put('V', 5);
values.put('X', 10);
values.put('L', 50);
values.put('C', 100);
values.put('D', 500);
values.put('M', 1000);
int total = 0;
for (int i = 0; i < s.length(); i++) {
int v = values.get(s.charAt(i));
if (i + 1 < s.length() && v < values.get(s.charAt(i + 1))) {
total -= v;
} else {
total += v;
}
}
return total;
}
}class Solution {
public int romanToInt(String s) {
Map<Character, Integer> values = new HashMap<>();
values.put('I', 1);
values.put('V', 5);
values.put('X', 10);
values.put('L', 50);
values.put('C', 100);
values.put('D', 500);
values.put('M', 1000);
int total = 0;
for (int i = 0; i < s.length(); i++) {
total += values.get(s.charAt(i));
}
for (int i = 0; i < s.length() - 1; i++) {
if (values.get(s.charAt(i)) < values.get(s.charAt(i + 1))) {
total -= 2 * values.get(s.charAt(i));
}
}
return total;
}
}✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED