Solving the Roman Numeral to integers Algorithm

Tim Rinkerman
3 min readJan 23, 2021

This problem (number 13 on leetcode) is as follows

Although somewhat simple in approach and solution, I really enjoyed solving this problem because it reenforced certain concepts of variables and values in javascript.

First it’s important to establish the logic of how the numbers are formed with roman numerals. For the most part it is pretty straight forward, you simply add the values of the numbers represented by letters to get your value. So “III” is 3 because “I” represents 1 and 1+1+1 or I+I+I= 3. However if the order of the letters is a value being followed by a greater value, say IV (I = 1, V = 5) you actually subtract the lower value from the higher value, so IV = 4.

Having this figured out I like to pseudo code the logic as I feel this is the harder part to visualize and the rest of the solving is about making the pieces fit.

Next, we have established in the directions what the values are going to be equal to, a key if you will. Realizing it was a key my first inclination was to set all of the letters as variables equal to an integer value. But our problem passes a string into our function and we cant exactly say “let “I” = 1. Understanding this means we can create a key with that letter by making an object hash where we assign a value of a number to a letter.

The last thing to note before we get to coding is that while iterating through our string, we know when we have to add vs subtract, how to figure out what string letter is equal to a number, but we must also keep a counter while we do the math.

To start

var romanToInt = function(s) {

let count = 0
let numHash = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000}};

Next we know we have to iterate over our string and this is where our logic that we first thought of comes into play. If the string value of the current index (i) is less than the string value of the next index (i+1) then subtract the value of current index from the next index, then add whatever that is value is to the current count and make sure to skip ahead one index with i++ so you don’t do additional math to the value you just subtracted from. Otherwise, simply add the value of the current index to the count you have started. Finally return said count.

var romanToInt = function(s) {

let count = 0
let numHash = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000}


for(let i = 0; i < s.length; i++){
if(numHash[s[i]] < numHash[s[i + 1]]){
let num = numHash[s[i + 1]] - numHash[s[i]]
count += num
i++
}else{
count += numHash[s[i]]
}
}
return count
};

While this is a fairly easy concept to get, “Create a key and compare values to it, do some math” I really enjoyed solving it because it really cements the basic concepts of variables and thoughtfully implementing logic. Often times I find myself looking at a problem and if it seems to easy I tend to glaze over it but I think this problem demonstrates the importance of taking the time to slow down and really think a problem through, even if it looks easy.

Happy Hacking!

--

--