# JAVA solution using map

• Step 1. create a map of basic roman numerals to their integer values
Step 2. Parse the string for each roman numeral & do the following
2.1 Add the integer if the current numeral is > next
2.2 Subtract the integer value of current numeral < next

``````    public int romanToInt(String s) {
//create  a map of roman numerals to integer values
Map<Character, Integer> romanToInt = new HashMap<Character, Integer>();
romanToInt.put('I', 1);
romanToInt.put('V', 5);
romanToInt.put('X', 10);
romanToInt.put('L', 50);
romanToInt.put('C', 100);
romanToInt.put('D', 500);
romanToInt.put('M', 1000);

//convert string to characters of roman numerals
int num = 0;
for(int i = 0; i < s.length(); i++){
char current = s.charAt(i);
if(i+1 < s.length()){
char next = s.charAt(i+1);
if(romanToInt.containsKey(current) && romanToInt.containsKey(next)){
if(romanToInt.get(current) < romanToInt.get(next))
num = num - romanToInt.get(current);
else
num = num + romanToInt.get(current);
}
}
else
num = num + romanToInt.get(current);
}

return num;
}
}
``````

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