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.
1 <= s.length <= 15s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M')[1, 3999].The algorithm iterates through the string once, so the time complexity is linear in the length of the input string.
The space complexity is constant because the size of the roman_dict is fixed and does not depend on the input size.
[Explain your approach here - describe the algorithm, intuition, and key insights]
The algorithm iterates through the string once, so the time complexity is linear in the length of the input string.
The space complexity is constant because the size of the roman_dict is fixed and does not depend on the input size.
In this approach, we iterate through the string from front to back. For each character, we compare its value with the value of the next character. If the current character's value is less than the next character's value, it means we need to subtract the current character's value from the result (this accounts for cases like "IV" or "IX"). Otherwise, we simply add the current character's value to the result.
The algorithm iterates through the string once, so the time complexity is linear in the length of the input string.
The space complexity is constant because the size of the roman_dict is fixed and does not depend on the input size.
In this approach, we use a while loop to iterate through the string. We check if the current character is less than the next character. If it is, we add the difference of the next character and the current character to the result and skip the next character by incrementing i by 2. If it is not, we simply add the value of the current character to the result and move to the next character by incrementing i by 1.
| Approach | Rating | Time Complexity | Space Complexity | Advantages | Disadvantages |
|---|---|---|---|---|---|
| Iterating from Back to Front | Intuitive subtraction logic when reading right-to-left | Requires explicit checks for each subtraction case | |||
| Iterating from Front to Back | Cleanest and most concise logic — simply compare current vs next value | None | |||
| Iterating from Front to Back using a While Loop | Skips two characters at once for subtraction pairs, slightly fewer iterations | Slightly more complex loop control with manual index management |
The optimal approach for this problem is Solution 2: Iterating from Front to Back, which achieves time complexity and space complexity. It uses the simplest logic: if the current value is less than the next value, subtract it. otherwise, add it. This makes the code concise and easy to understand.