What is required?
Given a roman numeral, convert it to integer.
Discussion
Roman numerals are a numeral system that was used in ancient Rome and other parts of the Roman Empire. They are based on a combination of letters from the Latin alphabet. Here are the basic symbols used in Roman numerals and their corresponding values
| Symbol | Value |
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
These symbols can be combined to represent different numbers. The rules for constructing Roman numerals are as follows:
- Symbols are combined from left to right, and the values are added together to get the total value.
- For example, “VIII” represents 5 + 1 + 1 + 1 = 8.
- If a smaller value symbol appears before a larger value symbol, the smaller value is subtracted from the larger value.
- For example, “IV” represents 5 – 1 = 4.
- Similarly, “IX” represents 10 – 1 = 9.
- A symbol can be repeated up to three times in a row to represent its value.
- For example, “III” represents 1 + 1 + 1 = 3.
- However, the symbols for 5, 50, 500 (V, L, D) cannot be repeated.
- To represent larger numbers, a horizontal bar is placed above a symbol, indicating multiplication by 1000.
- For example, “M” represents 1000, and “MM” represents 1000 + 1000 = 2000.
- The largest number that can be represented using Roman numerals is 3,999 (MMMCMXCIX).
Roman numerals do not have a symbol for zero or negative numbers.
Discussion
Lets dive into the algorithm logic and explain each step
- Initialize a variable
finalAnswerto hold the resulting integer value - Iterate over the characters of the input string
sfrom right to left using a for loop. - Inside the loop, a switch statement is used to handle each Roman numeral character.
- For each character, the corresponding value is added to the
finalAnswervariable based on the following rules:- ‘M’: Add 1000 to
finalAnswer. - ‘D’: Add 500 to
finalAnswer. - ‘C’: Add 100 to
finalAnswerif the currentfinalAnsweris less than 500; otherwise, subtract 100 fromfinalAnswer. - ‘L’: Add 50 to
finalAnswer. - ‘X’: Add 10 to
finalAnswerif the currentfinalAnsweris less than 50; otherwise, subtract 10 fromfinalAnswer. - ‘V’: Add 5 to
finalAnswer. - ‘I’: Add 1 to
finalAnswerif the currentfinalAnsweris less than 5; otherwise, subtract 1 fromfinalAnswer.
- ‘M’: Add 1000 to
- After iterating through all the characters in the input string, return the
finalAnsweras the resulting integer value.
Time Complexity
The time complexity of this code is O(n), where n is the length of the input string s. This is because the code iterates through each character of the input string once. The switch statement has constant time complexity for each case.
Java code implementation
public int romanToInt(String s) {
int finalAnswer = 0;
for (int i = s.length() - 1; i >= 0; i--) {
switch (s.charAt(i)) {
case 'M':
finalAnswer += 1000;
break;
case 'D':
finalAnswer += 500;
break;
case 'C':
finalAnswer += 100 * (finalAnswer >= 500 ? -1 : 1);
break;
case 'L':
finalAnswer += 50;
break;
case 'X':
finalAnswer += 10 * (finalAnswer >= 50 ? -1 : 1);
break;
case 'V':
finalAnswer += 5;
break;
case 'I':
finalAnswer += (finalAnswer >= 5 ? -1 : 1); // I = 1 in romans
break;
default:
break;
}
}
return finalAnswer;
}
Python implementation

C++ implementation
int romanToInt(string s) {
int finalAnswer = 0;
for (int i = s.length() - 1; i >= 0; i--) {
switch (s[i]) {
case 'M':
finalAnswer += 1000;
break;
case 'D':
finalAnswer += 500;
break;
case 'C':
finalAnswer += 100 * (finalAnswer >= 500 ? -1 : 1);
break;
case 'L':
finalAnswer += 50;
break;
case 'X':
finalAnswer += 10 * (finalAnswer >= 50 ? -1 : 1);
break;
case 'V':
finalAnswer += 5;
break;
case 'I':
finalAnswer += (finalAnswer >= 5 ? -1 : 1);
break;
default:
break;
}
}
return finalAnswer;
}
C# implementation
public int RomanToInt(string s) {
int finalAnswer = 0;
for (int i = s.Length - 1; i >= 0; i--)
{
switch (s[i])
{
case 'M':
finalAnswer += 1000;
break;
case 'D':
finalAnswer += 500;
break;
case 'C':
finalAnswer += 100 * (finalAnswer >= 500 ? -1 : 1);
break;
case 'L':
finalAnswer += 50;
break;
case 'X':
finalAnswer += 10 * (finalAnswer >= 50 ? -1 : 1);
break;
case 'V':
finalAnswer += 5;
break;
case 'I':
finalAnswer += (finalAnswer >= 5 ? -1 : 1);
break;
default:
break;
}
}
return finalAnswer;
}
Check out my other article of finding the Length Of The Longest Substring Without Repeating Characters here
Our Facebook page is here. You can also find our youtube channel here.

[…] Check out my other article or Algorithm – Roman To Integer here […]