Roman Number to Integer | Practice | GeeksforGeeks

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
Example 1:
Input:
s = V
Output: 5

Example 2:
Input:
s = III 
Output: 3

Your Task:
Complete the function romanToDecimal() which takes a string as input parameter and returns the equivalent decimal number. 
Expected Time Complexity: O(|S|), |S| = length of string S.
Expected Auxiliary Space: O(1)
Constraints:
1<=roman no range<=3999
 
0
aakasshuit4 days ago
        int ans = 0;
        HashMap <Character, Integer> map = new HashMap<>();
        
        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);
        
        for(int i=0;i<str.length();i++){
            if(i < str.length()-1 && map.get(str.charAt(i)) < map.get(str.charAt(i+1))){
            ans-=map.get(str.charAt(i));
            }else{
            ans+=map.get(str.charAt(i));
            }
        }
        return ans;
0
manthanpatil9124 days ago
#Python solution
class Solution:   def romanToDecimal(self, S):       n = len(S)       sum = 0       d = {'I' :1,       'V' :5,       'X' :10,       'L' :50,       'C' :100,       'D' :500,       'M' :1000}        for i in range(n):           if i == n-1:               sum += d[S[i]]           elif d[S[i]] >= d[S[i+1]]:               sum += d[S[i]]           elif d[S[i]] < d[S[i+1]]:               sum -= d[S[i]]       return sum 
#main function
0
spiderman694 days ago
using unordered map
 int romanToDecimal(string &str) {
        // code here
        unordered_map<char,int> mp;
        mp['I'] =1;
        mp['V'] = 5;
        mp['X'] = 10;
        mp['L'] = 50;
        mp['C'] = 100;
        mp['D'] = 500;
        mp['M'] = 1000;
        
        int res;
        res = mp[str[0]];
        if(str.length()==1)
        {
            return res;
        }
    
         
        for(int i=1;i<str.length();i++)
        {
            if(mp[str[i-1]]>=mp[str[i]])
            {
                res+=mp[str[i]];
            }
                else
                {
                    res+= (mp[str[i]]- 2*mp[str[i-1]]);
                }
        }
        return res;
    }
0
eeidgodg
This comment was deleted.
0
moninmodi6 days ago
class Solution { public:   int romanToDecimal(string &s) {       // code here       vector<int> v;   for(int i=0;i<s.length();i++){       if(s[i]=='I')           v.push_back(1);       else if(s[i]=='V')           v.push_back(5);       else if(s[i]=='X')           v.push_back(10);       else if(s[i]=='L')           v.push_back(50);       else if(s[i]=='C')           v.push_back(100);       else if(s[i]=='D')           v.push_back(500);       else           v.push_back(1000);   }   int ans=0;   for(int i=v.size()-1;i>=0;i--){       if(v[i]>v[i-1]&&i!=0){           ans+=v[i]-v[i-1];           --i;       }       else           ans+=v[i];   }   return ans;   }};
+1
bhuvanandro6 days ago
C++Solution (Using Unordered Map)
Time Taken : 0.01/1.12
 
int romanToDecimal(string &str) {       // code here       if(str.length()==0){           return 0;       }       unordered_map <char,int> mp;       mp['I']=1;       mp['V']=5;       mp['X']=10;       mp['L']=50;       mp['C']=100;       mp['D']=500;       mp['M']=1000;   int n =str.length();   char first=str[n-1];   int sum=mp[first];    for(int i=n-2;i>=0;i--){       char curr=str[i];       char prev=str[i+1];       if(mp[curr]>=mp[prev]){           sum+=mp[curr];       }       else{           sum-=mp[curr];       }   }   return sum;   
   }//FN
 
 
-1
shivamdubeyagra81 week ago
//       Time  =  0.11/1.13  (Java Solution)
 
class Solution {   public int romanToDecimal(String str) {       // code here       HashMap<Character, Integer> roman = new HashMap<>();              roman.put('I',1);       roman.put('V', 5);       roman.put('X', 10);       roman.put('L', 50);       roman.put('C', 100);       roman.put('D', 500);       roman.put('M', 1000);              int integer = 0;       int n = str.length();              if(n == 1)           return roman.get(str.charAt(0));              for(int i=0; i<n; i++)       {           if(i<n-1 && roman.get(str.charAt(i))<roman.get(str.charAt(i+1)))           {               integer += roman.get(str.charAt(i+1)) - roman.get(str.charAt(i));               i++;           }           else               integer += roman.get(str.charAt(i));       }       return integer;   }}
0
emandalbe191 week ago
Java solution using HashMap and iterating the string from last
class Solution {
    // Finds decimal value of a given roman numeral
    public int romanToDecimal(String str) {
        HashMap<Character, Integer> map = new HashMap<>();
        
        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 n = str.length();
        if(n==1){
            return map.get(str.charAt(0));
        }
        int total = map.get(str.charAt(n-1));
        for(int i=n-2; i>=0; i--){
            char curr = str.charAt(i);
            char next = str.charAt(i+1);
            
            if(map.get(curr) >= map.get(next)){
                total += map.get(curr);
            }
            else{
                total -= map.get(curr);
            }
            
        }
        
        return total;
        
        
    }
}

0
rajkishan3881 week ago
class Solution { public:   int romanToDecimal(string &str) {       int sum=0;              for(int i=0;i<str.length();i++){           char c=str[i];           if(c=='I'){               sum=sum+1;           }           else if(c=='V'){               sum=sum+5;                              if(str[i-1]=='I'){                   sum=sum-2*1;               }                                             }           else if(c=='X'){               sum=sum+10;               if(str[i-1]=='V'){                   sum=sum-2*5;               }               else if(str[i-1]=='I'){                   sum=sum-2*1;               }                                         }           else if(c=='L'){               sum=sum+50;               if(str[i-1]=='X'){                   sum=sum-2*10;               }               if(str[i-1]=='V'){                   sum=sum-2*5;               }               else if(str[i-1]=='I'){                   sum=sum-2*1;               }                          }            else if(c=='C'){               sum=sum+100;                if(str[i-1]=='L'){                    sum=sum-2*50;                }                if(str[i-1]=='X'){                   sum=sum-2*10;               }               if(str[i-1]=='V'){                   sum=sum-2*5;               }               else if(str[i-1]=='I'){                   sum=sum-2*1;               }                          }            else if(c=='D'){               sum=sum+500;                if(str[i-1]=='C'){                    sum=sum-2*100;                }                if(str[i-1]=='L'){                    sum=sum-2*50;                }                if(str[i-1]=='X'){                   sum=sum-2*10;               }               if(str[i-1]=='V'){                   sum=sum-2*5;               }               else if(str[i-1]=='I'){                   sum=sum-2*1;               }           }             else if(c=='M'){               sum=sum+1000;                 if(str[i-1]=='D'){                     sum=sum-2*500;                 }                 if(str[i-1]=='C'){                    sum=sum-2*100;                }                if(str[i-1]=='L'){                    sum=sum-2*50;                }                if(str[i-1]=='X'){                   sum=sum-2*10;               }               if(str[i-1]=='V'){                   sum=sum-2*5;               }               else if(str[i-1]=='I'){                   sum=sum-2*1;               }           }                 }       return sum;   }};        
+2
ownway1 week ago
C++ Solution Time = (0.01/1.12) EASY  
 
int romanToDecimal(string &str) {       int l=str.length();       int ans=0;       for(int i=l-1;i>=0;i--){           if(str[i] == 'I' )           {               if(ans < 3)                   ans=ans+1;               else{                   ans=ans-1;               }           }                                    else if(str[i] == 'V'){               if(ans <10)                   ans=ans+5;               else{                   ans=ans-5;               }           }                          else if(str[i] == 'X')           {               if(ans <50)                   ans=ans+10;               else{                   ans=ans-10;               }           }                                         else if(str[i] == 'L')            {               if(ans <100)                      ans=ans+50;               else{                     ans=ans-50;               }           }                                                else if(str[i] == 'C')            {               if(ans <500)                      ans=ans+100;               else{                    ans=ans-100;               }           }                                                   else if(str[i] == 'D')              {               if(ans <1000)                      ans=ans+500;               else{                  ans=ans-500;               }           }
           else if(str[i] == 'M')           {               if(ans <1000)                       ans=ans+1000;               else{                   ans=ans+1000;               }           }  
       }       return ans;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.