Thursday, July 25, 2024
Given a roman numeral, convert it to an integer.
func romanToInt(s string) int {
// rune to their integer values
romanValues := map[rune]int{
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
}
result := 0
prevValue := 0
// iterate over each character (rune) in the string
for _, char := range s {
// integer value for the current symbol
currentValue := romanValues[char]
// value to the result
result += currentValue
// handle a subtraction case
if currentValue > prevValue {
// if current value is greater than previous value,
// subtract twice the previous value:
// once to cancel the previous addition, and once for the actual subtraction
result = result - (2 * prevValue)
}
// next iteration
prevValue = currentValue
}
return result
}