Roman Numeral to Integer Conversion
The Roman numeral system is a fascinating ancient numeral system that uses letters to represent numbers. Converting these Roman numerals to their corresponding integer values is a common problem that appears in various contexts, from historical simulations to programming puzzles. Your task is to build a function that takes a Roman numeral string as input and returns its integer equivalent.
Problem Description
You need to implement a function that converts a Roman numeral string into its integer representation. Roman numerals are formed by combining specific symbols, each representing a value. The standard symbols and their corresponding values are:
- 'I' = 1
- 'V' = 5
- 'X' = 10
- 'L' = 50
- 'C' = 100
- 'D' = 500
- 'M' = 1000
The conversion involves understanding that symbols are generally added together. However, there's a special rule: when a symbol of smaller value appears before a symbol of larger value, it signifies subtraction. For example, 'IV' represents 4 (5 - 1) and 'IX' represents 9 (10 - 1). This subtraction rule only applies to specific pairs:
- '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.
Your function should correctly handle these additive and subtractive rules to produce the accurate integer value.
Key Requirements:
- The input will be a string representing a valid Roman numeral.
- The output should be an integer.
Expected Behavior:
- The function should iterate through the Roman numeral string and apply the conversion rules.
- It should correctly handle cases where symbols are added and cases where subtraction is implied.
Edge Cases to Consider:
- Single-character Roman numerals (e.g., "I", "V", "X").
- Roman numerals with only additive combinations (e.g., "III", "XII", "LXX").
- Roman numerals involving multiple subtractive combinations (e.g., "MCMXCIV").
- The largest possible valid Roman numeral within typical constraints (e.g., "MMMCMXCIX").
Examples
Example 1:
Input: "III"
Output: 3
Explanation: The Roman numeral "III" is composed of three 'I's, each representing 1. Therefore, 1 + 1 + 1 = 3.
Example 2:
Input: "LVIII"
Output: 58
Explanation: 'L' is 50, 'V' is 5, and 'III' is 3. Combining them gives 50 + 5 + 3 = 58.
Example 3:
Input: "MCMXCIV"
Output: 1994
Explanation:
'M' = 1000
'CM' = 900 (1000 - 100)
'XC' = 90 (100 - 10)
'IV' = 4 (5 - 1)
Total: 1000 + 900 + 90 + 4 = 1994
Constraints
- The input Roman numeral string will be between 1 and 15 characters in length, inclusive.
- The input string will only contain the characters: 'I', 'V', 'X', 'L', 'C', 'D', 'M'.
- The input string is guaranteed to represent a valid Roman numeral according to standard rules.
- The output integer will be within the range of a standard 32-bit signed integer.
- Your solution should aim for an efficient time complexity, ideally linear with respect to the length of the input string.
Notes
Consider how you might iterate through the Roman numeral string. You might find it helpful to look ahead at the next character to determine if a subtraction rule applies. A mapping from Roman numeral characters to their integer values will be essential. Remember that the subtraction rule is very specific and only applies to the pairs mentioned in the problem description.