class Solution: def romanToInt(self, s: str) -> int: dic = {} dic["I"] = 1 dic["V"] = 5 dic["X"] = 10 dic["L"] = 50 dic["C"] = 100 dic["D"] = 500 dic["M"] = 1000 dic["IV"] = 4 dic["IX"] = 9 dic["XL"] = 40 dic["XC"] = 90 dic["CD"] = 400 dic["CM"] = 900 number = 0 index = 0 while 1: if index +1 == len(s): number = number + dic[s[index]] return number elif index > len(s)-1: return number else: s_key = s[index : index+2] if s_key in dic: number = number + dic[s_key] index = index + 2 else: number = number + dic[s[index]] index = index + 1