The primary challenge in counting Roman numerals arises when a numeral acts as a subtractive value rather than an additive one. For instance, in “IV”, the value of “I” (1) is subtracted from the value of “V” (5). Apart from these cases, you simply sum the values of all the numerals.
Subtractive numerals can be recognized because they appear before a larger numeral. To handle this, we track two variables: prev and curr. For IV, prev is I and curr is V. When prev is less than curr, we subtract prev. After this, we continue by adding prev.
publicintromanToInt(String s) {
// If the input string is null or empty, return 0if (s ==null|| s.length() == 0) {
return 0;
}
// Create a map to store Roman numeral symbols and their integer values Map<Character, Integer> map = Map.of(
'M', 1000,
'D', 500,
'C', 100,
'L', 50,
'X', 10,
'V', 5,
'I', 1
);
// Initialize sum to store the final integer valueint sum = 0;
// Initialize prev to store the value of the first Roman numeralint prev = map.get(s.charAt(0));
// Initialize curr to store the value of the current Roman numeralint curr = 0;
// Iterate over the string starting from the second characterfor (int i = 1; i < s.length(); i++) {
// Get the value of the current Roman numeral curr = map.get(s.charAt(i));
// If prev is less than curr, subtract prev from sum to handle the subtraction caseif (prev < curr) {
sum -= prev;
} else {
// Otherwise, add prev to sum sum += prev;
}
// Update prev to the value of the current Roman numeral prev = curr;
}
// Add the last processed Roman numeral value to sum// This takes care of adding the last numeral which was not included in the loop sum += prev;
// Return the final calculated integer valuereturn sum;
}
defromanToInt(s: str) -> int:
# If the input string is empty, return 0ifnot s:
return0# Create a dictionary to store Roman numeral symbols and their integer values roman_map = {
'M': 1000,
'D': 500,
'C': 100,
'L': 50,
'X': 10,
'V': 5,
'I': 1 }
# Initialize sum to store the final integer value sum =0# Initialize prev to store the value of the first Roman numeral prev = roman_map[s[0]]
# Iterate over the string starting from the second characterfor i in range(1, len(s)):
# Get the value of the current Roman numeral curr = roman_map[s[i]]
# If prev is less than curr, subtract prev from sum to handle the subtraction caseif prev < curr:
sum -= prev
else:
# Otherwise, add prev to sum sum += prev
# Update prev to the value of the current Roman numeral prev = curr
# Add the last processed Roman numeral value to sum# This takes care of adding the last numeral which was not included in the loop sum += prev
# Return the final calculated integer valuereturn sum