Roman To Integer-LeetCode

Melissa Guachun
3 min readDec 9, 2021

Examples are your friends in disguise!

Photo by Anne Nygård on Unsplash

On my leetcode journey I came across another classic challenge. The Roman to Integer coding challenge taught me a valuable lesson in paying attention to the given examples. Given examples and tables are there for a reason. They assist you in understanding the logic behind a function, hinting you towards the answer.

The directions for the challenge was simple, convert the roman numeral into an integer. But the table and the examples were lengthy which honestly overwhelmed me at first. But after carefully evaluating the table of symbol value conversion table along with particular examples, the answer seemed more attainable than I thought.

The table is there for a reason, for the purpose of converting. Given an input, we have to go through each key value pair, which in this case would be the symbol and value, to determine the output. If we transcribe this logic, we can see parallels to mapping a collection of elements by key value pairs.

var romanToInt = function(s) {const table = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 100
}
let result = 0; //we are setting up a result to have something to return

Now, lets take a step back and refer to the examples. If we look at example 2:

Input: "IV"
Output: 4
// I : 1,
// V : 5,
//IV : 4 because I is less than the character after it, being V
// 5 - 1 = 4

We are combining characters that represent numbers in this example. According to the table, I is less than the number after it, which is V. So we must subtract I from V.

Lets look at example 4:

Input: s = "LVIII"
Output: 58

If we follow our logic from example 2, we know if a number is less than the number after it, we must subtract. In example 4, L : 50, which is greater than the number after it which is V: 5, so we must use inverse operations and use addition. We combine LV to make a new value being 55. Since LV is greater than I, we continue to add the rest to make a sum of 58.

So going back to our problem:

var romanToInt = function(s) {const table = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 100
}
let result = 0;

If we think of an input, we have to go through each numeral and determine if a number is greater than or less than the next number. We can translate our knowledge into code and use an if else statement to loop through our input of listed numerals.

var romanToInt = function(s) {const table = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 100
}
let result = 0;
for (let i = 0, i < s.length; i++) {
if (table [s[i]] < table[s[i + 1]]) {
result -= table[s[i]]
} else {result += table[s[i]]
}
}
return result
};

In the first part of the block we use a for loop because we want to run through our table of information. The if statement determines if the next roman numeral is larger than the first one, we have to subtract this number. After the else statement, we determine if the next roman numeral is smaller than the first one, we add them together. Finally, we return the result.

Conclusion:

This is only maybe, the third LeetCode problem I’ve done. I don’t expect visible progress overnight. I still needed time to try to come up with some solution before pouring over youtube for explanation videos. But I did feel that I was starting to pick up on some of the patterns my programming friends assured me I’d see. I knew mapping was involved in some way and I was able to see a pattern of logic within the examples that I hadn’t picked up before. As slow as this process is going to be, I try to remind myself that with every problem I’m going to learn something new. The biggest lesson I’ll probably learn from all this is a lesson in patience.

--

--

Melissa Guachun

Software Developer and visual artist based in NYC. Join me on my journey to coding enlightenment or a torrential mental breakdown, whichever comes first.