Skip to main content

13. Roman to Integer

easy

Convert a Roman-numeral string into its integer value. Tests string scanning + the subtractive notation rule (IV = 4, IX = 9) — a classic look-ahead exercise.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M with values 1, 5, 10, 50, 100, 500 and 1000. For example, 2 is written as II, 12 is XII, and 27 is XXVII. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: I before V (5) and X (10) to make 4 and 9; X before L (50) and C (100) to make 40 and 90; C before D (500) and M (1000) to make 400 and 900. Given a roman numeral, convert it to an integer.

Constraints

  • 1 <= s.length <= 15
  • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].

Examples

Example 1

Input
s = "III"
Output
3

Explanation: III = 3.

Example 2

Input
s = "LVIII"
Output
58

Explanation: L = 50, V= 5, III = 3.

Example 3

Input
s = "MCMXCIV"
Output
1994

Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Hints

Progressive — try the first before opening the next.

Hint 1

Build a lookup table from each symbol to its value (I=1, V=5, X=10, L=50, C=100, D=500, M=1000).

Hint 2

Walk left to right. Normally you add the current symbol's value to a total.

Hint 3

When the current symbol's value is LESS than the next symbol's value, subtract instead of add. That handles IV, IX, XL, XC, CD, CM.

Hint 4

Alternative slick approach: walk right to left. Keep a running max-seen-so-far; if the current value is < max-seen, subtract, else add.

Solution approach

Reveal approach

Map each symbol to its integer value. Walk the string left to right. For position i, look at s[i] and s[i+1]: if value(s[i]) < value(s[i+1]), this is a subtractive pair (IV, IX, XL, XC, CD, CM) so subtract value(s[i]) from the total; otherwise add value(s[i]). The last character is always added (no next char to compare). O(n) time on the input length, O(1) space — the symbol map is constant. The right-to-left variant gives the same result with slightly less code: walk backward, track the previous (larger) value, and add or subtract based on comparison.

Complexity

Time
O(n)
Space
O(1)

Related patterns

  • math
  • string-scan

Related problems

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Amazon
  • Microsoft
  • Bloomberg
  • Apple
  • Adobe

Practice these live with InterviewChamp.AI

Drill Roman to Integer and Math problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →