Convert a Roman numeral to an Integer
This post will discuss how to convert a Roman numeral to the corresponding integer in C++, Java, and Python. Assume valid Roman numeral as input.
The Roman numerals system uses seven different symbols to represent numbers. These are I, V, X, L, C, D, and M, standing for 1, 5, 10, 50, 100, 500, and 1000, respectively. The Roman numerals are in the range [1, 3999], the minimum Roman numeral being I and the maximum Roman numeral being MMMCMXCIX.
For example,
Output: 7
Explanation: V = 5, I = 1
Input: XXIX
Output: 29
Explanation: X = 10, I = 1
Input: CLX
Output: 160
Explanation: C = 100, L = 50, X = 10
The idea is very simple: evaluate the Roman numeral from left to right, keeping a simple rule in mind: when a digit is placed before another digit of higher value, we add the difference between the digits to the result. For example, consider a Roman numeral LXLVI, where the corresponding integer values are L = 50, X = 10, V = 5, I = 1. It can be evaluated as follows:
= 50 + (50 – 10) + 5 + 1
= 50 + 40 + 5 + 1
= 96
It should be noted that putting a lower-valued X before a higher-valued L produces (L - X). Following is the C++, Java, and Python implementation based on the idea:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <iostream> #include <unordered_map> #include <string> using namespace std; // Function to convert Roman numeral to corresponding integer value int getIntegerValue(string s) { // base case: empty string if (s.empty()) { return 0; } // Roman to integer mapping unordered_map<char, int> mapping = { {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000} }; // initialize the sum with 0 int value = 0; // start the loop with start for (int i = 0; i < s.length(); i++) { // if the current char has less value than its following char, // decrement the sum by the corresponding Roman number for that char if (i + 1 < s.length() && mapping[s[i]] < mapping[s[i + 1]]) { value -= mapping[s[i]]; } // otherwise, increment the sum by the corresponding Roman number else { value += mapping[s[i]]; } } return value; } int main() { string s = "XXIX"; cout << getIntegerValue(s); return 0; } |
Output:
29
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import java.util.Map; class Main { // Roman to integer mapping public static Map<Character, Integer> mapping = Map.ofEntries( Map.entry('I', 1), Map.entry('V', 5), Map.entry('X', 10), Map.entry('L', 50), Map.entry('C', 100), Map.entry('D', 500), Map.entry('M', 1000) ); // Function to convert Roman numeral to corresponding integer value public static int getIntegerValue(String s) { // base case if (s == null || s.isEmpty()) { return 0; } // initialize the sum with 0 int value = 0; // start the loop with start for (int i = 0; i < s.length(); i++) { // if the current char has less value than its following char, // decrement the sum by the corresponding Roman number for that char if (i + 1 < s.length() && mapping.get(s.charAt(i)) < mapping.get(s.charAt(i + 1))) { value -= mapping.get(s.charAt(i)); } // otherwise, increment the sum by the corresponding Roman number else { value += mapping.get(s.charAt(i)); } } return value; } public static void main(String[] args) { String s = "XXIX"; System.out.println(getIntegerValue(s)); } } |
Output:
29
Python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Function to convert Roman numeral to corresponding integer value def getIntegerValue(s): # base case if not s: return 0 # Roman to integer mapping mapping = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 } # initialize the sum with 0 value = 0 # start the loop with start for i in range(len(s)): # if the current char has less value than its following char, # decrement the sum by the corresponding Roman number for that char if i + 1 < len(s) and mapping[s[i]] < mapping[s[i + 1]]: value -= mapping[s[i]] # otherwise, increment the sum by the corresponding Roman number else: value += mapping[s[i]] return value if __name__ == '__main__': s = 'XXIX' print(getIntegerValue(s)) |
Output:
29
The time complexity of the above solution is O(n), where n is the length of the Roman string literal.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)