easyStringHash TableArrays & Hashing

Roman to Integer

easyTime: O(n)Space: O(1)

Recognize the pattern

Roman numeral to integersymbol valuessubtraction rule

Brute force idea

A straightforward first read of Roman to Integer is this: Map each symbol, add values left to right — misses subtraction cases. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

A calmer way to see Roman to Integer is this: If current symbol < next symbol, subtract instead of add. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout Roman to Integer is this: When a smaller value appears before a larger one, subtract it (IV = 4, not 6). If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

The trap in Roman to Integer usually looks like this: Not handling the subtraction rule — compare each symbol with the next one. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Arrays & Hashing Pattern