math and geometry August 15, 2022

Roman to integer

Time O(n) Space O(1) Open original problem

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)