import ( "fmt" ) func romanToInt(s string) int { total := 0 for i := 0; i < len(s); { dec, size := decodeRomanNumeral(s[i:]) total += dec i += size } return total } var m map[string]int = map[string]int { "I": 1, "IV": 4, "V": 5, "IX": 9, "X": 10, "XL": 40, "L": 50, "XC": 90, "C": 100, "CD": 400, "D": 500, "CM": 900, "M": 1000, } func decodeRomanNumeral(s string) (int, int) { oneLetter := string(s[0]) twoLetter := "" if len(s) > 1 { twoLetter = s[:2] } if dec, ok := m[twoLetter]; ok { return dec, 2 } if dec, ok := m[oneLetter]; ok { return dec, 1 } panic(fmt.Sprintf("invalid character in %s", s)) } Complexity Time: O(n) Space: O(1)