Excel Sheet Column Title LeetCode solution in Python

1
class Solution:
2
def convertToTitle(self, num: int) -> str:
3
# We make this lookup list, having A-Z in ascending order
4
alpha = [
5
chr(x) for x in range(ord("A"), ord("Z") + 1)
6
] # range(65, 90+1) -> 91-65 = 26
7
res = ""
8

9
while num > 0:
10
res += alpha[
11
(num - 1) % 26
12
] # since 0 indexed list, num-1 % 26 gives the index of ch in alpha
13
num = (num - 1) // 26
14
return res[::-1]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0