easyMath / GeometryPure DSA~13 min
Roman Tier Decode
A legacy licensing system labels plan tiers with Roman numerals. Convert a Roman-numeral tier string into its integer value.
Problem
Given a Roman numeral string s, convert it to an integer. Symbols are I=1, V=5, X=10, L=50, C=100, D=500, M=1000; when a smaller symbol precedes a larger one it is subtracted (e.g. IV = 4, IX = 9).
Input
A valid Roman numeral string s (1 ≤ |s| ≤ 15) in the range 1..3999.
Output
An integer — the decoded value.
Constraints
- Input is guaranteed to be a valid Roman numeral
- Subtractive pairs: IV, IX, XL, XC, CD, CM
- Value range 1..3999
Examples
Example 1
Input
s = "LVIII"
Output
58
L=50, V=5, III=3.
Example 2
Input
s = "MCMXCIV"
Output
1994
M=1000, CM=900, XC=90, IV=4.