Roman to Integer - LeetCode Problem

Given a roman numeral, convert it to an integer. (LeetCode link)
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Solution:
  1. Define Integer values on each Roman characters
  2. Replace the Ramon characters for 4, 9, 40, 90, 400, 900 (6 replacements)
  3. Iterate over the Ramon characters and get the values from 1st step
Code (JavaScript):

var romanToInt = function(s) {
   
    var total = 0;
    var romanData = {
        "I" : 1,
        "V" : 5,
        "X" : 10,
        "L" : 50,
        "C" : 100,
        "D" : 500,
        "M" : 1000
    };
    s = s.replace("IV","IIII"); //4
    s = s.replace("IX","VIIII"); //9
    s = s.replace("XL","XXXX"); //40
    s = s.replace("XC","LXXXX"); //90
    s = s.replace("CD","CCCC"); //400
    s = s.replace("CM","DCCCC"); //900
    var sLen = s.length;
    var i = 0;
    for(; i < sLen; i++){
        total += romanData[s[i]]
    }
    return total;
};

s - Roman string (input)

Comments