Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
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.
Constraints:
1 <= s.length <= 15scontains only the characters('I', 'V', 'X', 'L', 'C', 'D', 'M').- It is guaranteed that
sis a valid roman numeral in the range[1, 3999].
Approach: Left-to-Right Scan with Subtraction Rule
Algorithm
- Map each Roman symbol to its value
- Iterate through the string from left to right
- If the current symbol is smaller than the next symbol, subtract its value (e.g.,
IV= 4) - Otherwise, add its value
- Return the total
Time & Space Complexity
- Time Complexity: O(n) - single pass over the string
- Space Complexity: O(1) - fixed-size lookup map
Java Implementation
import java.util.HashMap;
import java.util.Map;
public class RomanToInteger {
private static final Map<Character, Integer> VALUES = new HashMap<>() {{
put('I', 1);
put('V', 5);
put('X', 10);
put('L', 50);
put('C', 100);
put('D', 500);
put('M', 1000);
}};
/**
* Convert a roman numeral to an integer.
* @param s Roman numeral string
* @return Integer value
*/
public static int romanToInt(String s) {
int total = 0;
for (int i = 0; i < s.length(); i++) {
int current = VALUES.get(s.charAt(i));
if (i + 1 < s.length() && current < VALUES.get(s.charAt(i + 1))) {
total -= current;
} else {
total += current;
}
}
return total;
}
// Test method
public static void main(String[] args) {
System.out.println("Input: s = \"III\"");
System.out.println("Output: " + romanToInt("III")); // Expected: 3
System.out.println("Input: s = \"LVIII\"");
System.out.println("Output: " + romanToInt("LVIII")); // Expected: 58
System.out.println("Input: s = \"MCMXCIV\"");
System.out.println("Output: " + romanToInt("MCMXCIV")); // Expected: 1994
}
}
Example Walkthrough
For s = "MCMXCIV":
- M: 1000 < next C? No. total=1000
- C: 100 < next M (1000)? Yes. total=1000-100=900
- M: 1000 < next X? No. total=1900
- X: 10 < next C (100)? Yes. total=1900-10=1890
- C: 100 < next I? No. total=1990
- I: 1 < next V (5)? Yes. total=1990-1=1989
- V: 5, last. total=1994
Key Points
- Subtraction Rule: A smaller symbol before a larger one means subtraction
- Guaranteed Valid Input: No need to validate the roman numeral format
- Single Pass: Compare each symbol with the next to decide add or subtract
- O(1) Space: A fixed lookup table is used
- Range: Valid values are always within
[1, 3999]