Roman numerals are represented by seven different symbols:&nbsp;I, V, X, L, C, D and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example,&nbsp;2 is written as II&nbsp;in Roman numeral, just two ones added together. 12 is written as&nbsp;XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:


	I can be placed before V (5) and X (10) to make 4 and 9.&nbsp;
	X can be placed before L (50) and C (100) to make 40 and 90.&nbsp;
	C can be placed before D (500) and M (1000) to make 400 and 900.


Given a roman numeral, convert it to an integer.

&nbsp;
Example 1:

Input: s = "III"
Output: 3
Explanation: III = 3.


Example 2:

Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.


Example 3:

Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 15
	s contains only&nbsp;the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
	It is guaranteed&nbsp;that s is a valid roman numeral in the range [1, 3999].


class Solution:
    def romanToInt(self, s: str) -> int:
        roman = {
            "I": 1,
            "V": 5,
            "X": 10,
            "L": 50,
            "C": 100,
            "D": 500,
            "M": 1000
        }

        sum = 0;
        for i in range(0, len(s) - 1):
            curr = roman[s[i]]
            nxt = roman[s[i + 1]]
            if curr < nxt:
                sum -= curr
            else:
                sum += curr
        sum += roman[s[-1]]
        return sum
class Solution {
    public int romanToInt(String s) {
        int res=0;
        // Let s = "IV" after traversing string res will be 6
        // Let s= "IX" after traversing string res will be 11
        for(int i=0;i<s.length();i++){
            switch(s.charAt(i)){
                case 'I': res=res+1;
                    break;
                case 'V': res=res+5;
                    break;
                case 'X': res+=10;
                    break;
                case 'L': res+=50;
                    break;
                case 'C': res+=100;
                    break;
                case 'D': res+=500;
                    break;
                case 'M': res+=1000;
                    break;
            }
        }
        // Since s= "IV" it satisfies first condition and 2 is subtracted from res. res=4
        // Since s= "IX" it satisfies first condition and 2 is subtracted from res. res=9
        if(s.contains("IV")||s.contains("IX"))
            res=res-2;
        if(s.contains("XL")||s.contains("XC"))
            res=res-20;
        if(s.contains("CD")||s.contains("CM"))
            res=res-200;

        return res;
    }
}
class Solution {
public:
    int romanToInt(string s){
        unordered_map<char, int> T = { { 'I' , 1 },
        { 'V' , 5 },
        { 'X' , 10 },
        { 'L' , 50 },
        { 'C' , 100 },
        { 'D' , 500 },
        { 'M' , 1000 } };
        int sum = T[s.back()];
        for (int i = s.length() - 2; i >= 0; --i){
            if (T[s[i]] < T[s[i + 1]]) sum -= T[s[i]];
            else sum += T[s[i]];
        }
        return sum;
    }
};
var romanToInt = function(s) {
      const sym = {
        'I': 1,
        'V': 5,
        'X': 10,
        'L': 50,
        'C': 100,
        'D': 500,
        'M': 1000
    }

    let result = 0;

    for (let i = 0; i < s.length; i++) {
        const cur = sym[s[i]];
        const next = sym[s[i + 1]];

        if (cur < next) {
            result += next - cur;
            i++;
        } else {
            result += cur;
        }
    }

    return result;
};
Roman to Integer