Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0171 - Excel Sheet Column Number
    0171 - Excel Sheet Column Number
    0171 - Excel Sheet Column Number
    0171 - Excel Sheet Column Number

    0171 - Excel Sheet Column Number

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/excel-sheet-column-number/

    Solution 1 - Base-26 Conversion (Left to Right)

    Treat the column title as a base-26 number where A=1, B=2, ..., Z=26. Process each character from left to right, multiplying the running total by 26 and adding the current character's value.

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

    Solution 2 - Using enumerate() and pow() (Right to Left)

    Process the string from right to left. Each character contributes its value multiplied by 26 raised to the power of its position index (0-indexed from the right).

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0171.1 - Solution 1 - Base-26 Conversion (Left to Right) """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def titleToNumber(self, columnTitle: str) -> int:
            """Excel Sheet Column Number Function"""
            result = 0
            for char in columnTitle:
                result = result * 26 + (ord(char) - ord('A') + 1)
            return result
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().titleToNumber("A"))       # Expected: 1
        print(Solution().titleToNumber("AB"))      # Expected: 28
        print(Solution().titleToNumber("ZY"))      # Expected: 701
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0171.2 - Solution 2 - Using enumerate() and pow() (Right to Left) """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def titleToNumber(self, columnTitle: str) -> int:
            """Excel Sheet Column Number Function"""
            result = 0
            for i, char in enumerate(reversed(columnTitle)):
                result += (ord(char) - ord('A') + 1) * (26 ** i)
            return result
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().titleToNumber("A"))       # Expected: 1
        print(Solution().titleToNumber("AB"))      # Expected: 28
        print(Solution().titleToNumber("ZY"))      # Expected: 701
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()