“Roman Integer” Javascript Solution Cheat Sheet

Fadi Tillman
3 min readJun 5, 2021

--

The “Remove Roman Integer” challenge is in the easy category and is a good start for understanding more challenging algorithms. I will focus on explaining a solution that works and not on the O time and space complexity.

Challenge: “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 one’s 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.” LeetCode

Note:

//7 symbols, each Roman symbols have an integer value// Usually, the value is the sum of symbols values from left to right//6 exceptions were we subtract the left symbol to get the value// Convert roman symbols to an integer

Example :

input “III“return 3input “IX”return 9

Explanation:

//Loop through the symbols//and addition the symbols values //if it is one exception case, subtract the value from left side.

1.Create an object with the key-value pairs

var symbols = {“I”: 1,“V”: 5,“X”: 10,“L”: 50,“C”: 100,“D”: 500,“M”: 1000}

2.Loop through the symbols “s”

for(var i = 0; i < s.length; i++) {}

3.Subtract for exceptions cases

  • “I can be placed before V (5) and X (10) to make 4 and 9.”leetCode
if(s[i] == “I” && (s[i+1] == “V”|| s[i+1] == “X”) ) {

Return the symbol value minus the symbol value before it, then increments i

sumNum += symbols[s[i+1] ] — symbols[s[i]]i++
  • “X can be placed before L (50) and C (100) to make 40 and 90.”LeetCode
}else if(s[i] == “X” && (s[i+1] == “L”|| s[i+1] == “C”) ){sumNum += symbols[s[i+1] ] — symbols[s[i]]i++
  • “C can be placed before D (500) and M (1000) to make 400 and 900.”LeetCode
} else if(s[i] == “C” && (s[i+1] == “D”|| s[i+1] == “M”) ){sumNum += symbols[s[i+1] ] — symbols[s[i]]i++

4.Sum symbols values

var sumNum = 0;sumNum += symbols[s[i]]

5.Return roman symbols converted to an integer

}
return sumNum

};

Solution:

var romanToInt = function(s) {var symbols = {“I”: 1,“V”: 5,“X”: 10,“L”: 50,“C”: 100,“D”: 500,“M”: 1000}var sumNum = 0;for(var i = 0; i < s.length; i++) {if(s[i] == “I” && (s[i+1] == “V”|| s[i+1] == “X”) ) {sumNum += symbols[s[i+1] ] — symbols[s[i]]i++}else if(s[i] == “X” && (s[i+1] == “L”|| s[i+1] == “C”) ){sumNum += symbols[s[i+1] ] — symbols[s[i]]i++} else if(s[i] == “C” && (s[i+1] == “D”|| s[i+1] == “M”) ){sumNum += symbols[s[i+1] ] — symbols[s[i]]i++} else {sumNum += symbols[s[i]]}}return sumNum};

--

--