Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Roman Numeral To Integer

Easy
0/40
Average time to solve is 15m
profile
Contributed by
71 upvotes
Asked in companies
CiscoUberAdobe

Problem statement

You are given a string 's' that represents a Roman number. Convert the Roman number to an integer and return it.


Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M.


Table of values:
Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000
For example:
3 is written as III in Roman numeral, just three ones added together. 13 is written as XIII, which is simply X + III. The number 25 is written as XXV, which is XX + V 
Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line contains a string ‘roman’ representing the number's roman number representation.
Output Format
The only line contains a single integer denoting the integer value of the given roman number.
Note:
Do not print anything, just return an integer denoting the equivalent integer of the given roman number

It is guaranteed that the string input is one of the characters of I, V, X, L, C, D, M.

It is guaranteed that the integer value of the given roman number will not exceed 3999.
Sample Input 1:
XII
Sample Output 1:
12
Explanation For Sample Input 1 :
We know that ‘X’ is 10, and we have 2 ‘I’ after it. Therefore the number is 12
Sample Input 2:
XC
Sample Output 2:
90
Constraints:
1 <= roman.length <= 15

Time limit: 1 second
Follow Up:
Can you solve this in O(N) time?
Full screen
Console