MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / romanToDecimal

Method romanToDecimal

RomanToInteger.java:3–29  ·  view source on GitHub ↗
(String s)

Source from the content-addressed store, hash-verified

1class Solution {
2 // Finds decimal value of a given roman numeral
3 public int romanToDecimal(String s) {
4 HashMap<Character,Integer> map = new HashMap<>();
5 map.put('I',1);
6 map.put('V',5);
7 map.put('X',10);
8 map.put('L',50);
9 map.put('C',100);
10 map.put('D',500);
11 map.put('M',1000);
12
13 int n=s.length();
14 int output=0;
15 for(int i=0;i<n;i++)
16 {
17 if(i<n-1 && map.get(s.charAt(i))<map.get(s.charAt(i+1)))
18 {
19 output+= map.get(s.charAt(i+1))-map.get(s.charAt(i));
20 i++;
21 }
22 else
23 {
24 output+=map.get(s.charAt(i));
25 }
26 }
27 return output;
28
29 }
30}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected