Convert a Roman Number to Decimal using Hashmap in Java
Given a Roman numeral, the task is to find the corresponding decimal value.
Note: Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M.
- Loop through each character in the string containing the Roman numerals.
- Compare the value of the current roman symbol with the value of the roman symbol to its right. If the current value is greater than or equal to the value of the symbol to the right, add the current symbol’s value to the total. If the current value is smaller than the value of the symbol to the right, subtract the current symbol’s value from the total.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)