DSA Tracker

Easy

Roman to Integer

An easy Strings problem included in Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Strings
Sheets
1
Core for
13 roles
Platform
LeetCode

The problem

Convert a Roman numeral string into its corresponding integer value.

Example 1

Input
s="III"
Output
3
Why
Roman numeral I equals 1, so III sums to 1 + 1 + 1 = 3.

Example 2

Input
s="LVIII"
Output
58
Why
L = 50, V = 5, and III = 3, combining into 50 + 5 + 3 = 58.

Example 3

Input
s="MCMXCIV"
Output
1994
Why
M = 1000, CM = 900, XC = 90, and IV = 4, resulting in 1000 + 900 + 90 + 4 = 1994.

Constraints

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

How to think about it

Updated 2026-09-09

Roman numerals generally descend in value from left to right, adding together. The only exception is subtractive notation: whenever a smaller symbol precedes a larger one, that smaller symbol subtracts from the total instead of adding. Looking at each symbol relative to its right-hand neighbor resolves whether to add or subtract in a single linear pass.

Approaches, worst first

  1. Subtractive pattern replacement

    time O(n) · space O(n)

    Replace subtractive pairs such as "IV" with "IIII" and "IX" with "VIIII" across the string, then sum the independent values of all individual characters. Functional and simple, but creates temporary string copies across multiple replacement passes.

  2. Right-to-left backward accumulation

    time O(n) · space O(1)

    Iterate backward from the end of the string while tracking the maximum value encountered so far. If the current symbol has a value smaller than the maximum seen to its right, subtract it from the running total; otherwise, add it and update the maximum value.

  3. Left-to-right neighbor comparisonWrite this one

    time O(n) · space O(1)

    Scan indices from 0 to length - 1. If the value at index i is strictly less than the value at index i + 1, subtract value[i] from the accumulator; otherwise, add value[i]. The final character always adds because it has no right neighbor.

Where people lose marks · 3
  • Reading past string bounds when inspecting index i + 1 on the last character; ensure bounds checking or handle the last symbol outside the loop.
  • Missing subtractive pairs like 'CD' (400) or 'CM' (900) by only testing for 'IV' and 'IX'.
  • Assuming characters can be subtracted more than once consecutively; standard Roman numeral grammar forbids patterns like 'IIV' for 3.

The theory behind it

Strings — the ground this problem stands on. All Strings problems

What Strings is

A string is an ordered necklace of text characters, like letters printed along a ribbon of paper. Each character sits at an exact numeric slot, holding a glyph such as a letter, punctuation mark, or digit. In many programming languages, ribbons cannot be edited after creation, meaning changing a single character requires pressing an entirely new ribbon from scratch.

When to reach for it

Reach for string techniques when inputs consist of words, DNA sequences, serialized data formats, or sentences. Clues include questions testing palindromes, anagram matches, substring patterns, parenthesis balancing, or character frequency counts. Whenever an algorithm asks to transform capitalization, parse structured tokens, or compute edits between two phrases, string representations are the core subject.

How the pattern works

Think of characters as small integer codes ranging across standard character sets. Frequency tables with fixed sizes often replace heavy hash maps when tallying occurrences. For search tasks, maintain rolling state using character indices or sliding borders. When building output text through repeated appends, accumulate pieces inside a mutable list or string builder rather than concatenating strings directly, avoiding quadratic copy overhead.

What each operation costs

OperationTime
read character by indexO(1)
concatenate two strings of total length nO(n)
compare two strings of length nO(n)
What usually goes wrong with Strings
  • Concatenating strings inside a loop using the plus operator, which silently creates full copies on each iteration and turns linear routines into quadratic slowdowns.
  • Assuming all characters fall strictly within lowercase English letters without validating spaces, uppercase variants, punctuation marks, or multi-byte unicode symbols.
  • Confusing substring length with end index when slicing, causing unexpected off-by-one truncations in languages that take length versus exclusive end position.

Which roles need this problem

Strings is a core topic for these 13 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 7 more roles, including Data Engineer, Data Analyst, Embedded / Firmware Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Strings problems

Problem set and role mapping as of .