13. Roman to Integer

 
Roman to Integer

Roman to Integer

Problem Description

Roman numerals are represented by seven different symbols: IVXLCD 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 from 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].
Approach: Loop with an object (hashmap)

For this super simple solution, we loop through the characters of the provided string, and for each one, we check whether that character is worth more or less than the next one (if the next one exists).
If it’s worth less, that means it is a deduction (for example, in the string “IV”, “I” is worth less (1) than “V” (5), so it is a deduction), and so this value is removed from the overall total.
Otherwise, it’s considered an addition, and added to the total:

Let`s Code It!

var romanToInt = function (string) {
    const numerals = {
        I: 1,
        V: 5,
        X: 10,
        L: 50,
        C: 100,
        D: 500,
        M: 1000,
    };

    const strLen = string.length;
    let total = 0;

    // Loop through the letters
    for (let i = 0; i < strLen; i++) {
        // Check if the current letter is followed by one with a higher value (indicating a deduction)
        if (i < strLen - 1 && numerals[string[i + 1]] > numerals[string[i]]) {
            // Remove the current letter's numeric value from the total
            total -= numerals[string[i]];
        } else {
            // Add the current letter's numeric value to the total
            total += numerals[string[i]];
        }
    }

    return total;
};

Note: I used the word hashmap in this solution’s title because that’s what most solutions for this problem use, however, hashmaps aren’t available in JavaScript natively (or rather, they are, but they’re just Objects or Maps).



Conclusion

That’s all folks! In this post, we solved LeetCode problem #13. Roman to Integer

I hope you have enjoyed this post. Feel free to share your thoughts on this.

You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.


In this blog, I have tried to solve leetcode questions & present the most important points to consider when improving Data structure and logic,  feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!

Comments

Popular Post