Roman to integer

August 15, 2022

math-and-geometry

Problem URL: Roman to integer

We can replace characters like IV or IX to IIII and VIIII. In that way, when we calculate our result, we can just translate the value to integer withour any prior calculations. We will have a hashmap to have translate all the roman numerals to integer values, and sum this up to return as result.

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

        s = s.replace('IV', 'IIII') \
             .replace('IX', 'VIIII') \
             .replace('XL', 'XXXX') \
             .replace('XC', 'LXXXX') \
             .replace('CD', 'CCCC') \
             .replace('CM', 'DCCCC')

        return sum(map(translations.get, s))

Time Complexity: O(n)
Space Complexity: O(1)