Roman to Integer is a question asked in many interviews including FAANG companies.  

The question for an algorithm is, 


Roman numerals are represented by seven different symbols: 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, 2 is written as II in Roman numeral, just two ones added together. 12 is written as 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. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.


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.



Constraints:

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



Let's start with the algorithm,


const romanToInt = function(s) {
  const Roman = {
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000
  }

  let result = 0
  for (let i = s.length - 1; i >= 0; i--) {
    const high = Roman[s[i]];
    const low = (Roman[s[i + 1]] || 0)
    result += (high < low ? -1 : 1) * high
  }

  return result
}


Here, We have created a array list representing Roman symbols with values in an array. We have decleared a variable result with default value of Zero. 

Now, we have for loop which starts from the length of word(LVIII) to zero in decrement order. Inside the loop we have two constants high and low, containing values high is containing first letter from right side of the word passing in Roman array to get the value of high like ( LVII,  we have 4 letters then i = 3, s[i] = I and Roman[I] becomes 1, so value of high is 1). 

Same goes wth low as word is LVII and i=3 and s[i+1] = null, then Roman[null] =null and low has condition Roman[s[i+1]]||0. So here low becomes 1.

Then we have result startement which is checking if hight is less than low then we will have negative value if high is more than low then we have positive value.

Let's take example and calculate that Roman to integer,

'MMMCMXCIX' is our Roman number,

- i=8, high(X) = 10, low(null) = 0, result = 10
- i=7, high(I) = 1, low(X) = 10, result = (-1) x 1 + 10=9
- i=6, high(C) = 100, low(I) = 1, result = (1) x 100 + 9 =109
- i=5, high(X) = 10, low(C) = 100, result = (-1) x 10 + 109 = -10 + 109 = 99
- i=4, high(M) = 1000, low(X) = 10, result = (1) x 1000 + 99 = 1099
- i=3,  high(C) = 100, low(M) = 1000, result = (-1) x 100 + 1099 = 999 
- i=2, high(M) = 1000, low(C) = 100, result = (1) x 1000 + 999 = 1999
- i=1, high(M) = 1000, low(M) = 1000, result = (1) x 1000 + 1999 = 2999
- i=0, high(M) = 1000, low(M) = 1000, result = (1) x 1000 + 2999 = 3999

MMMCMXCIX = 3999

I hope you understood the logic of the algorithm. If you have any doubt or any other way to solve this algorithm let us know in the comment section.