IBM | Off Campus Drive Technical support Associate | Bangalore | Coding Assessment | HackerRank

**Convert roman number to decimal number **

just like leetcode easy question

https://leetcode.com/problems/roman-to-integer/

Very Simple Solution in C++

class Solution {

public:

int romanToInt(string s) {

unordered_map<char,int> um={{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};

int i=0,res=0,sub=0,pre=0;

while(i<s.length())

{

    if(um[s[i+1]]>um[s[i]])
    {

        sub=um[s[i+1]]-um[s[i]];

        res+=sub;
        i=i+2;

    }
    else

    {

        res+=um[s[i]];
        i++;
    }
}
return res;

}
};

Comments (1)