Roman to Integer

Roman numerals use seven symbols with fixed values: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and M = 1000. Values are usually written from largest to…

Roman numerals use seven symbols with fixed values: `I` = `1`, `V` = `5`, `X` = `10`, `L` = `50`, `C` = `100`, `D` = `500`, and `M` = `1000`. Values are usually written from largest to smallest and **added** together. The exception is the **subtractive** form: when a smaller symbol appears immediately before a larger one, the smaller value is **subtracted**. The six subtractive pairs are `IV` (4), `IX` (9), `XL` (40), `XC` (90), `CD` (400), and `CM` (900). Given a valid Roman numeral string `s`, return its integer value. Indexing is **0-based**. **Example:** `"MCMXCIV"` = `M` (1000) + `CM` (900) + `XC` (90) + `IV` (4) = `1994`.

- `1 <= s.length <= 15` - `s` contains only the characters `I`, `V`, `X`, `L`, `C`, `D`, `M`. - `s` is guaranteed to be a valid Roman numeral in the range `1` to `3999`.

Example 1: input s = "III"; output 3. I + I + I = 1 + 1 + 1 = 3. No symbol is smaller than the one after it.

Example 2: input s = "LVIII"; output 58. L=50, V=5, III=3, so 50 + 5 + 3 = 58.

Example 3: input s = "MCMXCIV"; output 1994. M=1000, CM=900, XC=90, IV=4, so 1000 + 900 + 90 + 4 = 1994.

Category: Strings. Topic: strings. Difficulty: Easy. Patterns: string-processing.