Convert a Roman numeral string into its integer value.
You are given a string representing a Roman numeral. Convert it to the corresponding integer.
Roman numerals use the following symbols:
| Symbol | Value |
|---|---|
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
Normally, symbols are added from left to right. However, when a smaller value appears before a larger value, it is subtracted instead of added.
Return the integer value of the given Roman numeral.
s representing a Roman numeral.I, V, X, L, C, D, and M.1 <= s.length <= 15 is a reasonable interview constraint for this problem.s contains only valid Roman numeral characters.Example 1
Input
s = "III"
Output
3
Explanation
Each symbol adds 1, so 1 + 1 + 1 = 3.
Example 2
Input
s = "IV"
Output
4
Explanation
I comes before V, so it is subtracted: 5 - 1 = 4.
Example 3
Input
s = "MCMXCIV"
Output
1994
Explanation
M = 1000, CM = 900, XC = 90, and IV = 4, so the total is 1994.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.