Question:
Converting Roman Number to Integer

Summary:

Given a string in roman no format (s)  your task is to convert it to an integer . Various symbols and their values are given below.

I 1

V 5

X 10

L 50

C 100

D 500

M 1000


Solution:

//User function Template for javascript


/**

 * @param {string} str

 * @returns {number}

 */


class Solution {

    romanToDecimal(str){ 

        const romanNumerals = {

            M: 1000,

            CM: 900,

            D: 500,

            CD: 400,

            C: 100,

            XC: 90,

            L: 50,

            XL: 40,

            X: 10,

            IX: 9,

            V: 5,

            IV: 4,

            I: 1,

        };

        let result = 0;

        for (let i = 0; i < str.length; i++) {

            const currentSymbol = str[i];

            const nextSymbol = str[i + 1];

            if (romanNumerals[currentSymbol] < romanNumerals[nextSymbol]) {

              result -= romanNumerals[currentSymbol];

            } else {

              result += romanNumerals[currentSymbol];

            }

        }

        return result;

    }

}


The provided JavaScript code defines a class Solution with a method romanToDecimal. This method takes a Roman numeral represented as a string (str) and converts it to its decimal equivalent.


Code Explanation:

  • Create an object ‘romannumerals’ that maps Roman numeral symbols to their decimal values.

  • Initialize a variable result to 0, which will store the final decimal value.

  • Iterate through each character in the input string (str).

  • For each character, check its value in the “romanNumerals” object.

  • Compare the current symbol with the next symbol. If the current symbol has a smaller value than the next symbol, subtract the current symbol's value from the result (e.g., IV represents 4, where I is subtracted from V).

  • If the current symbol is not smaller than the next symbol, add its value to the result.

  • Continue the loop until all characters in the string are processed.

  • Return the final result, which represents the decimal equivalent of the input Roman numeral.

  • The code follows the rules of Roman numeral representation, where certain combinations of symbols are used to represent specific values. The algorithm iterates through the input string, adjusting the result based on the comparison of adjacent symbols to handle cases where subtraction is required.


Suggested blogs:

>Python Error Solved: load_associated_files do not load a txt file

>Python Error Solved: pg_config executable not found

>Set up Node.js & connect to a MongoDB Database Using Node.js

>Setting up a Cloud Composer environment: Step-by-step guide

>What is microservice architecture, and why is it better than monolithic architecture?

>What is pipe in Angular?

>What makes Python 'flow' with HTML nicely as compared to PHP?

>What to do when MQTT terminates an infinite loop while subscribing to a topic in Python?

>Creating custom required rule - Laravel validation

>How to configure transfers for different accounts in stripe with laravel?


Nisha Patel

Nisha Patel

Submit
0 Answers