# Solved in Java using Roman Numerals property of Previous and Current

• ``````    public int romanToInt(String s) {
int output=0;
int current=0;
int previous =0;
HashMap<Character,Integer> hashMap = new HashMap<Character,Integer>();
hashMap.put('I',1);

hashMap.put('V',5);

hashMap.put('X',10);
hashMap.put('L',50);
hashMap.put('C',100);
hashMap.put('D',500);
hashMap.put('M',1000);
previous= hashMap.get(s.charAt(0));
output=previous;
for(int i=1;i<s.length();i++)
{
current=hashMap.get(s.charAt(i));
if(current>previous)
{
output= output - previous + current-previous;
}
else
output+=current;
previous=current;

}

return output;

}
}```
We see the property that if a character is **larger** than its previous, it means their resultant value is the difference of the 2 values for sure. If its smaller than the previous value, then the result is the sum of the 2 values.
Eg; IV , I is smaller than V so V-I = 5-1 = 4 . VI , V is larger than I so V+I = 5+1 =6.
Take out the first array character and set it as the output and the previous. Now check the next character, if its larger, remove previous from the output and add the difference of previous and current. Otherwise just add the number to the output.``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.