Problem Definition
The objective is to translate a given valid Roman numeral string into its corresponding base-10 integer. The input will always represent a number within the range of 1 to 3999.
Roman Numeral Notation Rules
Roman numerals are constructed using seven primary symbols: I, V, X, L, C, D, and M, representing values 1, 5, 10, 50, 100, 500, and 1000 respectively. The conversion relies primarily on two positional principles:
- Addditive Principle: When a smaller or equal symbol appears after a larger one, their values are summed (e.g.,
VIequals 6). - Subtractive Principal: When a smaller symbol precedes a larger one, the smaller value is deducted from the larger (e.g.,
IVequals 4). Subtraction follows strict ordering rules and never skips adjacent numerical positions.
Algorithmic Strategy
A forward-pass approach requires inspecting the next character to decide whether to add or subtract the current value, which complicates boundary handling at the end of the string. A more efficient and cleaner method processes the sequence from right to left. By tracking the value of the previously encountered symbol, we can apply the subtraction rule deterministically: if the current symbol's value is strictly less than the previous symbol's value, it belongs to a subtractive pair and must be subtracted. Otherwise, it is added. This eliminates index arithmetic and simplifies state management.
Implementation
class Solution:
def romanToInt(self, s: str) -> int:
symbol_map = {
'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000
}
accumulated_value = 0
previous_value = 0
for char in reversed(s):
current_value = symbol_map[char]
if current_value < previous_value:
accumulated_value -= current_value
else:
accumulated_value += current_value
previous_value = current_value
return accumulated_value
Performance Characteristics
The routine iterates through the input string exactly once, executing constant-time dictionary lookups and arithmetic comparisons per character. This yields a linear time complexity of O(n), where n denotes the string length. Memory consumption remains constant at O(1), as the lookup table contains a fixed set of entries and only two integer variibles scale with execution state.