Roman To Integer Problem Solution

Roman To Integer Problem: 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 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 :

Input: s = “III” Output: 3 Explanation: III = 3

Roman To Integer Problem Solution

Problem Solution In Python

class Solution:
    def romanToInt(self, s: str) -> int:
        maps = {'I' : 1,'V' : 5,'X' : 10,
        'L' : 50,'C' : 100,'D' : 500,'M' : 1000}
        sums = 0
        i = 0
        while i < len(s)-1:
            if maps[s[i]] < maps[s[i+1]]:
                sums += maps[s[i+1]]-maps[s[i]]
                i += 1
            else:
                sums += maps[s[i]]
            i += 1
        if i != len(s):
            sums += maps[s[-1]]
        return sums

Problem Solution In Java

import java.util.*;
class Solution {
public int romanToInt(String s) {
HashMap<Character,Integer>map=new HashMap<Character,Integer>();

    map.put('I',1);
     map.put('V',5);
     map.put('X',10);
     map.put('L',50);
     map.put('C',100);
     map.put('D',500);
     map.put('M',1000);
    int result=map.get(s.charAt(s.length()-1));
    for(int i=s.length()-1;i>0;i--)
    {
        if(map.get(s.charAt(i))<=map.get(s.charAt(i-1)))
        {
            result+=map.get(s.charAt(i-1));
        }
        else
        {
            result-=map.get(s.charAt(i-1));
        }
    }
    return result;
}
}

Problem Solution In C++

class Solution {
int getTranslateNum(char s) {
    switch(s) {
        case 'I': return 1;
        case 'V': return 5;
        case 'X': return 10;
        case 'L': return 50;
        case 'C': return 100;
        case 'D': return 500;
        case 'M': return 1000;
        default: return 0;
    }
    return 0;
}
public:
    int romanToInt(string s) {
        if(s.empty()) return 0;
        int returnValue = 0;
        for(unsigned int i=0; i<s.size()-1; i++) {
            if(getTranslateNum(s[i]) < getTranslateNum(s[i+1])) {
                returnValue -= getTranslateNum(s[i]);
            } else {
                returnValue += getTranslateNum(s[i]);
            }
        }
        returnValue += getTranslateNum(s[s.size()-1]);
        return returnValue;
    }
};

Problem Solution In C

int getValue(const char * s){
    switch(*s) {
        case 'I': return (s[1] == 'V' || s[1] == 'X') ? -1 : 1;
        case 'X': return (s[1] == 'L' || s[1] == 'C') ? -10 : 10;
        case 'C': return (s[1] == 'D' || s[1] == 'M') ? -100 : 100;
        case 'V': return 5;
        case 'L': return 50;
        case 'D': return 500;
        case 'M': return 1000;
    }
    return 0;
}

int romanToInt(char * s){
    int result = 0; 
    
    for(;*s != '\0'; ++s) {
        result += getValue(s);
    }
    return result;
}

Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment