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:
Imagine you are given a long string of Roman numeral symbols and you need to calculate its total value by exhaustively examining every possible combination of adjacent symbols. The brute force approach involves breaking down the entire string into all possible overlapping pairs and single letters, testing various ways to group them, and manually evaluating every conceivable combination until the correct total is tallied.
Here's how the algorithm would work step-by-step:
def roman_to_integer(roman_string):
symbol_values = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
# We map every single symbol and adjacent pair to exhaustively evaluate all possible groupings
all_possible_groupings = []
string_index = 0
while string_index < len(roman_string):
if string_index + 1 < len(roman_string):
pair_string = roman_string[string_index:string_index+2]
all_possible_groupings.append(pair_string)
string_index += 2
else:
single_string = roman_string[string_index]
all_possible_groupings.append(single_string)
string_index += 1
accumulated_total = 0
# We test each evaluated grouping to determine if subtraction or addition rules apply
for current_group in all_possible_groupings:
if len(current_group) == 2:
first_value = symbol_values[current_group[0]]
second_value = symbol_values[current_group[1]]
if first_value < second_value:
accumulated_total += (second_value - first_value)
else:
accumulated_total += (first_value + second_value)
else:
accumulated_total += symbol_values[current_group]
# We return the exhaustive sum matching the required brute force interpretation logic
return accumulated_totalInstead of reading the Roman numeral from left to right and constantly second-guessing yourself, the optimal approach works backwards from right to left. By comparing each symbol to the one immediately to its right, you can instantly tell 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_mapping = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
# Begin at the very end of the string to evaluate the last symbol first.
last_character_index = len(roman_numeral_string) - 1
running_total = roman_value_mapping[roman_numeral_string[last_character_index]]
previous_symbol_value = running_total
for character_index in range(len(roman_numeral_string) - 2, -1, -1):
current_symbol_value = roman_value_mapping[roman_numeral_string[character_index]]
# If a smaller value precedes a larger one, subtract it per Roman rules.
if current_symbol_value < previous_symbol_value:
running_total -= current_symbol_value
else:
# Otherwise, add the value when symbols are in descending or equal order.
running_total += current_symbol_value
previous_symbol_value = current_symbol_value
return running_total| Case | How to Handle |
|---|---|
| Empty string input | Return zero immediately as an empty Roman numeral represents the value zero. |
| Single character string like I, V, or M | The loop executes once and correctly returns the direct integer value mapped to that single character. |
| Maximum length valid Roman numeral string | Iterate through the string in linear time O(n) which scales efficiently for any standard constraint size. |
| All identical characters like III or XXX | Always add the values since no smaller value precedes a larger value to trigger subtraction. |
| Subtractive notation combinations like IV, IX, XL, XC, CD, CM | Subtract the current character value from the total if its value is less than the next character value. |
| Mixed additive and subtractive sequences like MCMXCIV | The lookahead comparison correctly handles alternating addition and subtraction rules at each step. |
| Inputs containing invalid characters not in the Roman numeral set | A robust solution should throw an exception or return an error indicator when an unmapped character is encountered. |
| Integer overflow beyond standard limits | Standard Roman numerals generally do not exceed 3999, which fits safely within standard integer types without overflow. |