Logo
    Akhil Abraham
    Akhil Abraham
    0168 - Excel Sheet Column Title
    0168 - Excel Sheet Column Title

    0168 - Excel Sheet Column Title

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

    Solution 1 - Base-26 Conversion (Iterative)

    Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet (A=1, B=2, ..., Z=26, AA=27, AB=28, ...). This is essentially a 1-indexed base-26 number system. We repeatedly subtract 1 from columnNumber to convert it to 0-indexed, then take the remainder when divided by 26 to find the current character, and divide by 26 to move to the next digit. Characters are built from right to left, so we reverse at the end.

    TimeComplexity:O(log⁡26n)TimeComplexity: O(\log_{26} n)TimeComplexity:O(log26​n)
    SpaceComplexity:O(log⁡26n)SpaceComplexity: O(\log_{26} n)SpaceComplexity:O(log26​n)

    Solution 2 - Recursive Approach

    Same base-26 logic but implemented recursively. The base case is when columnNumber is 0, returning an empty string. Each recursive call processes one character by subtracting 1, finding the current letter via modulo, and recursing on the quotient. The recursion naturally builds the string from left to right.

    TimeComplexity:O(log⁡26n)TimeComplexity: O(\log_{26} n)TimeComplexity:O(log26​n)
    SpaceComplexity:O(log⁡26n)SpaceComplexity: O(\log_{26} n)SpaceComplexity:O(log26​n)
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    """ 0168.1 - Excel Sheet Column Title - Solution 1 - Base-26 Conversion (Iterative) """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def convertToTitle(self, columnNumber: int) -> str:
            """Convert column number to Excel title"""
            result = []
            while columnNumber > 0:
                columnNumber -= 1  # Convert to 0-indexed
                remainder = columnNumber % 26
                result.append(chr(remainder + ord('A')))
                columnNumber //= 26
            return ''.join(reversed(result))
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().convertToTitle(1))    # "A"
        print(Solution().convertToTitle(28))   # "AB"
        print(Solution().convertToTitle(701))  # "ZY"
        print(Solution().convertToTitle(26))   # "Z"
        print(Solution().convertToTitle(52))   # "AZ"
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0168.2 - Excel Sheet Column Title - Solution 2 - Recursive Approach """
    
    #####################################################################################
    # Imports
    #####################################################################################
    
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def convertToTitle(self, columnNumber: int) -> str:
            """Convert column number to Excel title using recursion"""
            if columnNumber == 0:
                return ""
            columnNumber -= 1  # Convert to 0-indexed
            return self.convertToTitle(columnNumber // 26) + chr(columnNumber % 26 + ord('A'))
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().convertToTitle(1))    # "A"
        print(Solution().convertToTitle(28))   # "AB"
        print(Solution().convertToTitle(701))  # "ZY"
        print(Solution().convertToTitle(26))   # "Z"
        print(Solution().convertToTitle(52))   # "AZ"
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()