Skip to content
Optimal Lab

Strings · 099Easy

Roman to Integer

Given a roman numeral string, convert it to an integer. Symbols: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. A smaller symbol placed before a larger one means subtraction (IV = 4, IX = 9, CM = 900).

Constraints

  • 1 <= s.length <= 15
  • s contains only the characters I, V, X, L, C, D, M.
  • The input is a valid roman numeral in the range [1, 3999].

Examples

romanToInt("III")        ->  3
romanToInt("I")          ->  1      // shortest possible input
romanToInt("XL")         ->  40     // a subtractive pair
romanToInt("LVIII")      ->  58     // L=50, V=5, III=3
romanToInt("MCMXCIV")    ->  1994   // M=1000, CM=900, XC=90, IV=4
romanToInt("MMMCMXCIX")  ->  3999   // the largest valid input

Edge cases

  • Only six subtractive pairs exist: IV, IX, XL, XC, CD, CM.
  • Repeated symbols simply add.
Show the target complexity (spoiler)
Naive
Hard-coding every two-character pair works but is verbose and error-prone.
Target
O(n) time, O(1) space — sum each symbol's value, subtracting instead of adding whenever a symbol is smaller than the one after it.

Solve it

By hand, in your editor. The runner checks answers and timing.

git clone https://github.com/sarmadkung/optimal-round
cd optimal-round
# open problems/15-strings/099-roman-to-integer.js
./practice c 099