Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0013 - Roman to Integer
    0013 - Roman to Integer
    0013 - Roman to Integer
    0013 - Roman to Integer

    0013 - Roman to Integer

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/roman-to-integer/

    Solution 1 - Right-to-Left Pass

    Traverse the Roman numeral string from right to left. If the current value is less than the previous value, subtract it (handles subtractive cases like IV, IX). Otherwise, add it.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Left-to-Right Pass

    Traverse the string from left to right. If the current value is less than the next value, subtract it. Otherwise, add it. This directly handles subtractive pairs like IV, IX, XL, etc.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0013.1 - Roman to Integer - Solution 1 - Right-to-Left Pass """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def romanToInt(self, s: str) -> int:
            """Roman to Integer Function"""
            roman = {'I': 1, 'V': 5, 'X': 10, 'L': 50,
                     'C': 100, 'D': 500, 'M': 1000}
            result = 0
            prev = 0
            for char in reversed(s):
                curr = roman[char]
                if curr < prev:
                    result -= curr
                else:
                    result += curr
                prev = curr
            return result
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().romanToInt("III"))       # 3
        print(Solution().romanToInt("LVIII"))     # 58
        print(Solution().romanToInt("MCMXCIV"))   # 1994
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0013.2 - Roman to Integer - Solution 2 - Left-to-Right Pass """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def romanToInt(self, s: str) -> int:
            """Roman to Integer Function"""
            roman = {'I': 1, 'V': 5, 'X': 10, 'L': 50,
                     'C': 100, 'D': 500, 'M': 1000}
            result = 0
            for i in range(len(s)):
                if i + 1 < len(s) and roman[s[i]] < roman[s[i + 1]]:
                    result -= roman[s[i]]
                else:
                    result += roman[s[i]]
            return result
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().romanToInt("III"))       # 3
        print(Solution().romanToInt("LVIII"))     # 58
        print(Solution().romanToInt("MCMXCIV"))   # 1994
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()