function romanToInt(s: string): number {
const romanMap: { [key: string]: number } = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
};
let result = 0;
for (let i = 0; i < s.length; i++) {
const currentVal = romanMap[s[i]];
const nextVal = i + 1 < s.length ? romanMap[s[i + 1]] : 0;
if (currentVal < nextVal) {
result -= currentVal;
} else {
result += currentVal;
}
}
return result;
}
console.log(romanToInt("III"));
console.log(romanToInt("LVIII"));
console.log(romanToInt("MCMXCIV"));