Problem statement

https://leetcode.com/problems/excel-sheet-column-title/

Solution

Nothing special here, just 26-base numbers, but be careful, we start form 1 not from 0, so we need each time look at n-1.

Complexity

Time and space complexity is O(log(n)).

Code

class Solution:
    def convertToTitle(self, n):
        ans = ""
        while n > 0:
            n, q = divmod(n-1, 26)
            ans += chr(ord("A") + q)
            
        return ans[::-1]