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:
I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (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 <= 15s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').s is a valid roman numeral in the range [1, 3999].When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To convert a Roman numeral to an integer using a brute force approach, we can think of it as systematically processing the Roman numeral string from left to right, character by character, and accumulating the corresponding integer value.
Here's how the algorithm would work step-by-step:
def roman_to_integer_brute_force(roman_numeral_string): total_integer_value = 0 roman_map = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 } for current_index in range(len(roman_numeral_string)): current_roman_value = roman_map[roman_numeral_string[current_index]] # If this is not the last character, check if the next character has a larger value if current_index < len(roman_numeral_string) - 1: next_roman_value = roman_map[roman_numeral_string[current_index + 1]] # If the next value is greater, we have a subtractive case if next_roman_value > current_roman_value: # We need to subtract the current value twice and then add the next value # This effectively means we are adding the difference (next_roman_value - current_roman_value) total_integer_value -= current_roman_value # Add the current character's value to the total total_integer_value += current_roman_value return total_integer_valueThe key is to process the Roman numeral from right to left, which simplifies handling the subtractive rule. By comparing each numeral to the one immediately to its right, we can determine whether to add or subtract its value.
Here's how the algorithm would work step-by-step:
def roman_to_integer(roman_numeral_string): roman_value_map = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 } total_integer_value = 0 previous_symbol_value = 0 # Iterate from right to left to handle subtractive cases easily. for current_symbol in reversed(roman_numeral_string): current_symbol_value = roman_value_map[current_symbol] # If the current symbol's value is less than the previous one, subtract it. if current_symbol_value < previous_symbol_value: total_integer_value -= current_symbol_value else: # Otherwise, add the current symbol's value to the total. total_integer_value += current_symbol_value # Update the previous symbol's value for the next iteration. previous_symbol_value = current_symbol_value return total_integer_value| Case | How to Handle |
|---|---|
| Null or empty input string | The solution should return 0 for a null or empty input string, as there are no Roman numerals to convert. |
| Single character Roman numeral (e.g., 'I', 'V') | The solution should correctly map single characters to their corresponding integer values. |
| Roman numerals with subtractive notation (e.g., 'IV', 'IX', 'XL', 'XC', 'CD', 'CM') | The algorithm must be designed to handle these specific combinations by checking preceding characters. |
| Roman numerals with repeated characters (e.g., 'III', 'XX') | The solution should correctly sum the values of repeated characters. |
| Longest possible valid Roman numeral input | The solution should scale efficiently for longer strings, ensuring performance does not degrade significantly. |
| Invalid Roman numeral characters (e.g., 'A', 'Z', numbers) | The problem statement implies valid Roman numeral input; however, a robust solution might throw an error or return a specific indicator for invalid characters. |
| Roman numerals exceeding the standard representation (e.g., 'MMMM') | The standard Roman numeral system typically stops at 3999, so inputs beyond that might be considered out of scope or require specific handling if the problem allows for extensions. |
| All identical values in a sequence that implies subtraction (e.g., 'IIII' instead of 'IV') | The conversion logic should prioritize subtractive notation over simple summation for valid cases, assuming standard Roman numeral rules. |