I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution: defromanToInt(self,s): translation={ "I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000 } number=0 s = s.replace("IV", "IIII").replace("IX", "VIIII") s = s.replace("XL", "XXXX").replace("XC", "LXXXX") s = s.replace("CD", "CCCC").replace("CM", "DCCCC") for char in s: number +=translation[char] return number