DEV Community

Urfan Guliyev
Urfan Guliyev

Posted on

Leetcode - Roman to Integer (with JavaScript)

Today I am going to show how to solve the Leetcode Roman to Integer algorithm problem.

Here is the problem:
problem

There are seven symbols in the Roman numeral system. Iā€™m going to map each symbol to its value.

var romanToInt = function(s) {

    const sym = { 
        'I': 1,
        'V': 5,
        'X': 10,
        'L': 50,
        'C': 100,
        'D': 500,
        'M': 1000
    }

}

I then create a new variable to represent the integer converted from a roman numeral.

var romanToInt = function(s) {

    const sym = { 
        'I': 1,
        'V': 5,
        'X': 10,
        'L': 50,
        'C': 100,
        'D': 500,
        'M': 1000
    }

    let result = 0;

}

If the numerals are descending from left to right, largest to smallest, then they are added together. But if the numerals begin to ascend in order, then the larger number is subtracted by the preceding smaller number. For instance, in XVIV, X and V are added together to make 15. Because the I is followed by a larger number, V, it is subtracted from that number to make 4 and then added to the preceding XV to make 19.

Based on this rule, I will iterate from left to right. If the current symbol (cur) is less than the symbol to its right (next), then I will subtract the cur from the next and move 2 characters to the right. Otherwise, I will simply add them.

var romanToInt = function(s) {

    const sym = { 
        'I': 1,
        'V': 5,
        'X': 10,
        'L': 50,
        'C': 100,
        'D': 500,
        'M': 1000
    }

    let result = 0;

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

        if (cur < next){
            result += next - cur // IV -> 5 - 1 = 4
            i++
        } else {
            result += cur
        }
    }

    return result; 
};

Top comments (5)

Collapse
 
foxeyerinx profile image
Rinx

missing let for the i inside the for loop

Collapse
 
jungus80 profile image
Jungus80

why i++ in the if condition ?

Collapse
 
moosorkh profile image
Fortis Irdmousa

Because if the current symbol is smaller than the next one, for instance, 'CM', it means that the number is 900 (100 subtracted from 1000). Therefore, the index must jump one place ahead of the current symbol so that the loop avoids mistakenly including the 'M' from 'CM' in the addition. I hope it makes sense.

Collapse
 
developer3027 profile image
Developer3027

in Roman numbers a situation where you have a lower value proceeding a higher value like "IV" you subtract. In the case of "IV" you subtract I = 1 from V = 5 to get four. The current and next variables are created to evaluate just this condition. If condition is found then do the math and set the new result. Seeing as you used two of the strings, you need to advance one to begin the next evaluation. Notice that next is i+1. If you don't advance after the subtraction eval then the loop count will fall one behind and the math is wrong. Hope this helps.

Collapse
 
syahbes profile image
Shlomi Yahbes

Well done šŸ‘