Standard Bar Strings · Medium O(n) · O(1)
A heritage engraving workshop receives restoration orders whose dates are carved as Roman numeral bands, and the CNC controller needs plain integers before it can program a job. The shop follows the standard convention: a smaller symbol placed before a larger one is subtractive, everything else adds up. The controller parses the band left to right, adding symbol values and flipping to subtraction whenever a smaller symbol precedes a larger.
Input: A string s representing a Roman numeral, using the symbols I, V, X, L, C, D and M.
Output: The integer value the numeral denotes.
1 <= s.length <= 15s contains only the characters 'I', 'V', 'X', 'L', 'C', 'D', 'M's is guaranteed to be a valid Roman numeral in the range [1, 3999]Input: {"s":"III"}
Output: 3
Three additive I symbols, no subtractive pairs.
Input: {"s":"LVIII"}
Output: 58
L is 50, V is 5 and III is 3; no symbol precedes a larger one, so everything adds.