Roman to Integer in Python

class Solution:
    def romanToInt(self, s: str) -> int:
        # Mapping Roman symbols to values
        roman_map = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}

        # Initialize the integer variable to store the result
        total = 0

        # Iterate through the Roman numeral string
        for i in range(len(s)):
            # Check if the current value is less than the next value
            if i + 1 < len(s) and roman_map[s[i]] < roman_map[s[i + 1]]:
                # Subtract this value
                total -= roman_map[s[i]]
            else:
                # Add this value
                total += roman_map[s[i]]

        return total

PrevNext