Given a positive integer N, return its corresponding column title as it would appear in an Excel sheet.
For N =1 we have column A, for 27 we have AA and so on.
Note: The alphabets are all in uppercase.
Example 1:
Input:
N = 51
Output: AY
Your Task:
Complete the function ExcelColumn() which takes N as input and returns output string.
Expected Time Complexity: O(Log(N))
Expected Auxiliary Space: O(Log(N))
Constraints:
1 ≤ N ≤ 10^{7}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def ExcelColumn(self, N):
        #return required string
        #code here

Solutions:
class Solution:

	def ExcelColumn(self, N):
		temp = N
		st = ''
		while temp:
			lt = temp % 26
			if lt == 0:
				st = 'Z' + st
				temp = temp // 26 - 1
			else:
				st = chr(64 + lt) + st
				temp = temp // 26
		return st


class Solution:

	def ExcelColumn(self, N):
		arr = [0] * 26
		ans = ''
		while N:
			rem = N % 26
			if not rem:
				N = N // 26 - 1
				ans = 'Z' + ans
			else:
				N = N // 26
				ans = chr(rem - 1 + 65) + ans
		return ans


class Solution:

	def ExcelColumn(self, N):
		s = ''
		while N:
			x = 65 + (N - 1) % 26
			s = chr(x) + s
			N = (N - 1) // 26
		return s


class Solution:

	def ExcelColumn(self, N):
		res = ''
		if N <= 26:
			return chr(64 + N)
		q = N
		while q != 0:
			r = q % 26
			if r == 0:
				res = res + 'Z'
				q = q // 26 - 1
			else:
				res = res + chr(r + 64)
				q = q // 26
		return res[::-1]


class Solution:

	def ExcelColumn(self, N):
		title = ''
		while N > 0:
			N -= 1
			remainder = N % 26
			title = chr(remainder + ord('A')) + title
			N //= 26
		return title


class Solution:

	def ExcelColumn(self, n):
		d = {i - 64: chr(i) for i in range(65, 65 + 26)}
		ans = ''
		while n != 0:
			rem = n % 26
			if rem == 0:
				ans = d[26] + ans
				n -= 1
			else:
				ans = d[rem] + ans
			n //= 26
		return ans


class Solution:

	def ExcelColumn(self, N):
		chars26 = ['Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y']
		res = ''
		while N > 0:
			rem = N % 26
			res = chars26[rem] + res
			N = (N - 1) // 26
		return res


class Solution:

	def ExcelColumn(self, N):
		s = ''
		list1 = [chr(i + 64) for i in range(1, 27)]
		if N <= 26:
			return list1[N - 1]
		while N != 0:
			rem = N % 26
			if rem == 0:
				N = N // 26 - 1
			else:
				N //= 26
			s += list1[rem - 1]
		return s[::-1]


class Solution:

	def ExcelColumn(self, n):
		title = ''
		while n:
			n = n - 1
			title = chr(n % 26 + 65) + title
			n = n // 26
		return title


class Solution:

	def ExcelColumn(self, N):
		ret = []
		c = N
		while c:
			last_digit = c % 26
			if last_digit == 0:
				last_digit = 26
				c -= 26
			ret.append(chr(ord('A') + last_digit - 1))
			c = c // 26
		return ''.join(reversed(ret))


class Solution:

	def ExcelColumn(self, n):
		s = ''
		k = n
		while n > 26:
			k = n % 26
			if k == 0:
				s = s + 'Z'
			elif bool(s) == True:
				if s[-1] == 'Z':
					s = s + chr(63 + k)
				else:
					s = s + chr(64 + k)
			else:
				s = s + chr(64 + k)
			n = n // 26
		if k != 0:
			s = s + chr(64 + n)
		else:
			s = s + chr(63 + n)
		return s[::-1]


class Solution:

	def ExcelColumn(self, N):
		n = N
		string = ''
		while n > 0:
			rem = n % 26
			str1 = ''
			if rem == 0:
				str1 = 'Z'
				n = n // 26 - 1
				string = string + str1
			else:
				str1 = chr(rem - 1 + ord('A'))
				string = string + str1
				n = n // 26
		string = string[::-1]
		return string


class Solution:

	def ExcelColumn(self, N):
		res = []
		while N > 26:
			if N % 26 == 0:
				res.append(26)
				N = N // 26
				N -= 1
			else:
				res.append(N % 26)
				N = N // 26
		if N > 0:
			res.append(N)
		val = ''
		res = res[::-1]
		if len(res) == 1:
			return chr(64 + res[0])
		for i in range(len(res)):
			val += chr(64 + res[i])
		return val


class Solution:

	def ExcelColumn(self, N):
		ans = ''
		while N:
			rem = N % 26
			if rem == 0:
				ans += 'Z'
				N = N // 26 - 1
			else:
				ans = ans + chr(64 + rem)
				N = N // 26
		return ans[::-1]


class Solution:

	def ExcelColumn(self, N):
		s = ''
		while N > 0:
			res = N % 26
			if res == 0:
				s += 'Z'
				N = N // 26 - 1
			else:
				s += chr(ord('A') + (res - 1))
				N = N // 26
		s = s[-1::-1]
		return s


class Solution:

	def ExcelColumn(self, N):
		d = ['Z'] + [chr(i) for i in range(65, 65 + 25)]
		res = ''
		while N:
			x = N % 26
			if x == 0:
				res = 'Z' + res
				N = N // 26 - 1
			else:
				res = d[x] + res
				N = N // 26
		return res


class Solution:

	def ExcelColumn(self, N):
		ans = ''
		while N > 0:
			rem = N % 26
			if rem == 0:
				ans = 'Z' + ans
				N = N // 26 - 1
			else:
				a = chr(ord('A') + rem - 1)
				ans = a + ans
				N = N // 26
		return ans


class Solution:

	def ExcelColumn(self, N):
		if N <= 26:
			return chr(N + 64)
		res = ''
		while N >= 1:
			if N <= 26:
				res += chr(int(N) + 64)
				break
			rem = N % 26
			if rem >= 1:
				res += chr(64 + int(rem))
				N //= 26
			else:
				res += 'Z'
				N = N // 26 - 1
		return res[::-1]


class Solution:

	def ExcelColumn(self, N):
		s = ''
		while N != 0:
			N -= 1
			mod = N % 26
			s += chr(65 + mod)
			N = N // 26
		a = s[::-1]
		return a


from math import log

class Solution:

	def ExcelColumn(self, N):
		output = ''
		while N:
			N -= 1
			mod = N % 26
			output += chr(65 + mod)
			N = N // 26
		return output[::-1]


class Solution:

	def ExcelColumn(self, N):
		stg = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'

		def recursive(N):
			nonlocal stg
			if N < 27:
				return stg[N]
			remainder = N % 26
			value = N // 26
			if remainder == 0:
				return recursive(value - 1) + recursive(26)
			return recursive(value) + recursive(remainder)
		return recursive(N)


class Solution:

	def ExcelColumn(self, N):
		st = [0] * 100
		i = 0
		x = ''
		while N > 0:
			r = N % 26
			if r == 0:
				st[i] = 'Z'
				i += 1
				N = N // 26 - 1
			else:
				st[i] = chr(r - 1 + ord('A'))
				i += 1
				N = N // 26
		st = st[::-1]
		for k in range(len(st)):
			if st[k] != 0:
				x += st[k]
		return x


class Solution:

	def ExcelColumn(self, N):
		if N <= 26:
			return chr(N + 64)
		q = N // 26
		r = N % 26
		if r == 0:
			r = 26
			q -= 1
		return self.ExcelColumn(q) + chr(r + 64)


from string import ascii_uppercase

class Solution:

	def ExcelColumn(self, N):
		char = ' ' + ascii_uppercase
		ans = ''
		while N:
			if N % 26 != 0:
				ans += char[N % 26]
			else:
				ans += 'Z'
				N -= 1
			N //= 26
		return ans[::-1]


class Solution:

	def ExcelColumn(self, N):
		excel_dict = {}
		excel_dict[0] = 'Z'
		for i in range(1, 26):
			excel_dict[i] = chr(64 + i)
		ans = ''
		if N == 26:
			return 'Z'
		while N > 0:
			rem = N % 26
			if rem == 0:
				N = N // 26 - 1
			else:
				N = N // 26
			ans = excel_dict[rem] + ans
		return ans


class Solution:

	def ExcelColumn(self, N):
		alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		ans = ''
		if N < 26:
			return alphabets[N - 1]
		else:
			while N > 0:
				rem = N % 26
				ans = alphabets[rem - 1] + ans
				if rem == 0:
					N = N - rem - 1
				else:
					N = N - rem
				N = N // 26
			return ans


class Solution:

	def ExcelColumn(self, n):
		arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
		ans = ''
		if n == 26:
			return arr[25]
		while n > 0:
			z = n % 26
			ans = arr[z - 1] + ans
			if z == 0:
				n = n - z - 1
			else:
				n = n - z
			n = n // 26
		return ans


class Solution:

	def ExcelColumn(self, N):
		temp = N
		ans = str()
		while temp:
			t = temp % 26
			if t == 0:
				ans = 'Z' + ans
				temp = temp // 26 - 1
			else:
				ans = chr(64 + t) + ans
				temp = temp // 26
		return ans


class Solution:

	def ExcelColumn(self, N):
		res = ''
		y = N % 26
		while N > 26:
			N = N // 26
			if y == 0:
				res += chr(90)
				N = N - 1
			else:
				res += chr(65 + (y - 1))
			y = N % 26
		if N == 0:
			res += chr(65)
		else:
			res += chr(65 + (N - 1))
		return res[::-1]


class Solution:

	def ExcelColumn(self, N):
		char1 = []
		for i in range(65, 65 + 26):
			char1.append(chr(i))
		excel = ''
		while N > 26:
			c = N % 26
			excel = char1[c - 1] + excel
			N = N // 26
			if c == 0:
				N = N - 1
		excel = char1[N - 1] + excel
		return excel


class Solution:

	def ExcelColumn(self, N):
		letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		res = ''
		while N > 0:
			i = N % 26
			res = letters[i - 1] + res
			N = N // 26
			if i == 0:
				N -= 1
		return res


class Solution:

	def ExcelColumn(self, N):
		dic = {i: chr(64 + i) for i in range(1, 27)}
		if n < 27:
			return dic[n]
		res = []

		def answer(n):
			q = n // 26
			r = n % 26
			if r == 0:
				res.append('Z')
				q -= 1
			elif r != 0:
				res.append(dic[r])
			if q < 27:
				res.append(dic[q])
				return res
			answer(q)
		answer(n)
		return ''.join(res[::-1])


class Solution:

	def ExcelColumn(self, N):
		d = {i - 64: chr(i) for i in range(65, 65 + 26)}
		a = []

		def answer(n):
			q = n // 26
			r = n % 26
			if r == 0:
				a.append('Z')
				q = q - 1
			else:
				a.append(d[r])
			if q > 26:
				answer(q)
			elif q != 0:
				a.append(d[q])
			return a
		return ''.join(answer(n)[::-1])


class Solution:

	def ExcelColumn(self, N):
		r = ''
		S = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		while N > 0:
			N = N - 1
			r = S[N % 26] + r
			N = N // 26
		return r


class Solution:

	def ExcelColumn(self, n):
		lst = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
		s = ''
		while n > 0:
			k = n % 26 - 1
			s += lst[k]
			if k == -1:
				n -= 26
			else:
				n -= k
			n = n // 26
		return s[::-1]


class Solution:

	def ExcelColumn(self, N):
		temp = N
		res = ''
		ch = None
		dict_num = {}
		dict_num[0] = 'Z'
		for i in range(1, 26):
			dict_num[i] = chr(i + 64)
		while int(temp) > 0:
			x = temp % 26
			res = dict_num[x] + res
			if x == 0:
				temp = temp // 26 - 1
			else:
				temp = temp // 26
		return res


class Solution:

	def ExcelColumn(self, N):
		n = N
		s = ''
		d = {}
		count = 65
		for i in range(1, 27):
			d[i] = chr(count)
			count += 1
		if n in d:
			s = d[n]
			return s
		while n > 0:
			rem = n % 26
			if rem == 0:
				s = d[rem + 26] + s
				n = n - rem
				n = n // 26 - 1
				if n in d:
					s = d[n] + s
					break
			else:
				s = d[rem] + s
				n = n - rem
				n = n // 26
				if n in d:
					s = d[n] + s
					break
		return s


class Solution:

	def ExcelColumn(self, N):
		alpha = {}
		for i in range(1, 27):
			alpha[round(i / 26 % 1, 3)] = chr(64 + i)
		res = ''
		while N != 0:
			res = alpha[round(N / 26 % 1, 3)] + res
			if N % 26 == 0:
				N = N // 26 - 1
			else:
				N = N // 26
		return res


class Solution:

	def ExcelColumn(self, N):
		memo = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		return self.hashedMap(N, memo)

	def hashedMap(self, n, memo):
		if n < 26:
			return memo[n - 1]
		else:
			quotient = n // 26
			rem = n % 26
			if rem == 0:
				if quotient == 1:
					return memo[rem - 1]
				else:
					return self.hashedMap(quotient - 1, memo) + memo[rem - 1]
			else:
				return self.hashedMap(quotient, memo) + memo[rem - 1]


class Solution:

	def ExcelColumn(self, N):
		import string
		a = list(string.ascii_uppercase)
		if N < 26:
			return a[N - 1]
		else:
			(q, r) = (N // 26, N % 26)
			if r == 0:
				if q == 1:
					return a[r - 1]
				else:
					return Solution.ExcelColumn(self, q - 1) + a[r - 1]
			else:
				return Solution.ExcelColumn(self, q) + a[r - 1]


import math

class Solution:

	def ExcelColumn(self, N):
		str = ''
		while N > 0:
			rem = N % 26
			if rem == 0:
				str = 'Z' + str
				N = N // 26 - 1
			else:
				str = chr(64 + rem) + str
				N = N // 26
		return str


class Solution:

	def ExcelColumn(self, N):
		res = ''
		q = 0
		al = [chr(x) for x in range(ord('A'), ord('Z') + 1)]
		while N > 0:
			q = (N - 1) % 26
			res += al[q]
			N = (N - 1) // 26
		return res[::-1]


class Solution:

	def ExcelColumn(self, N):
		table = [chr(x) for x in range(ord('A'), ord('Z') + 1)]
		res = []
		while N > 0:
			res.append(table[(N - 1) % 26])
			N = (N - 1) // 26
		res.reverse()
		return ''.join(res)


class Solution:

	def ExcelColumn(self, N):
		s = ''
		while N:
			rem = N % 26
			if rem == 0:
				rem = 26
			s = chr(64 + rem) + s
			N -= rem
			N = N // 26
		return s


class Solution:

	def ExcelColumn(self, num):
		alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		string = ''
		while True:
			if num < 26:
				string = alpha[num - 1] + string
				break
			(q, r) = (num // 26, num % 26)
			if r == 0 and q == 1:
				string = alpha[r - 1] + string
				break
			elif r == 0 and q != 1:
				num = q - 1
				string = alpha[r - 1] + string
			else:
				num = q
				string = alpha[r - 1] + string
		return string


class Solution:

	def ExcelColumn(self, N):
		a = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
		d = {i + 1: a[i] for i in range(len(a))}
		d[0] = 'Z'
		ans = ''
		while N:
			t = N % 26
			ans += d[t]
			N = N // 26
			if t == 0:
				N -= 1
		return ans[::-1]


class Solution:

	def ExcelColumn(self, n):
		alphabets = {}
		i = 1
		j = 65
		while j <= 90:
			alphabets[i] = chr(j)
			i += 1
			j += 1
		res = ''
		while n > 0:
			rem = n % 26
			if rem == 0:
				rem = 26
				res += alphabets[rem]
				n = n - 1
			else:
				res += alphabets[n % 26]
			n //= 26
		return res[::-1]


from collections import deque

class Solution:

	def ExcelColumn(self, N):
		ans = deque()
		while N != 0:
			if N % 26 == 0:
				ans.appendleft(chr(64 + 26))
			else:
				ans.appendleft(chr(64 + N % 26))
			N = (N - 1) // 26
		return ''.join(ans)


class Solution:

	def ExcelColumn(self, N):
		arr = []
		for i in range(0, 26):
			arr.append(chr(65 + i))
		n = N
		res = ''
		while n > 0:
			n -= 1
			x = n % 26
			res += str(arr[x])
			n = n // 26
		res = res[::-1]
		return res


class Solution:

	def ExcelColumn(self, N):
		alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		d = {}
		sub = 26
		tot = 26
		rem = 1
		ans = []
		for i in range(26):
			d[i] = alpha[i]
		while N > 0:
			ans += [d[(N - 1) // rem % tot]]
			N -= sub
			sub *= tot
			rem *= tot
		ans = ans[::-1]
		return ''.join(ans)


alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def num_hash(num):
	if num < 26:
		return alpha[num - 1]
	else:
		(q, r) = (num // 26, num % 26)
		if r == 0:
			if q == 1:
				return alpha[r - 1]
			else:
				return num_hash(q - 1) + alpha[r - 1]
		else:
			return num_hash(q) + alpha[r - 1]

class Solution:

	def ExcelColumn(self, N):
		return num_hash(N)


class Solution:

	def ExcelColumn(self, N):
		strg = ''
		while N > 0:
			rem = N % 26
			if rem != 0:
				strg += chr(65 + rem - 1)
				N //= 26
			else:
				strg += 'Z'
				N = N // 26 - 1
		return strg[::-1]


class Solution:

	def ExcelColumn(self, N):
		res = ''
		while N:
			x = N % 26
			if x != 0:
				t = ''
				t += chr(ord('A') + x - 1)
				t += res
				res = t
				N = int(N / 26)
			else:
				t = ''
				t += 'Z'
				t += res
				N = int(N / 26 - 1)
				res = t
		return res


class Solution:

	def ExcelColumn(self, N):
		res = ''
		while N > 0:
			rem = N % 26
			if rem == 0:
				res += 'Z'
				N = N // 26 - 1
			else:
				res += chr(ord('A') + rem - 1)
				N = N // 26
		res = ''.join(list(res)[::-1])
		return res


import math

class Solution:

	def ExcelColumn(self, N):
		d = {i: e for (i, e) in enumerate('ZABCDEFGHIJKLMNOPQRSTUVWXYZ')}
		res = ''
		while N > 0:
			res = res + d[N % 26]
			if N % 26 == 0:
				N = (N - N % 26) // 26 - 1
			else:
				N = (N - N % 26) // 26
		return res[::-1]
		res = ''
		return res


class Solution:

	def ExcelColumn(self, N):
		chars = 26
		res = ''
		go = True
		mynum = N
		while mynum > 0:
			rem = mynum % chars
			if rem == 0:
				res = 'Z' + res
				mynum = mynum // chars - 1
			else:
				res = chr(ord('A') + (rem - 1)) + res
				mynum = mynum // chars
		return res


class Solution:

	def ExcelColumn(self, N):
		t = N
		result = ''
		while t != 0:
			it = t % 26
			if it == 0:
				result = 'Z' + result
				t = t // 26 - 1
			else:
				result = chr(64 + it) + result
				t = t // 26
		return result


class Solution:

	def ExcelColumn(self, N):
		n = N
		res = ''
		while n > 0:
			if n % 26 == 0:
				res += 'Z'
				n = n // 26 - 1
			else:
				res += chr(96 + n % 26).upper()
				n = n // 26
		res = res[::-1]
		return res


class Solution:

	def ExcelColumn(self, n):
		s = ''
		i = 0
		while n > 0:
			rem = n % 26
			if rem == 0:
				s += 'Z'
				i += 1
				n = n // 26 - 1
			else:
				s += chr(rem - 1 + ord('A'))
				i += 1
				n = n // 26
		return s[::-1]


class Solution:

	def ExcelColumn(self, N):
		s = ''
		while N:
			x = N % 26
			if x is 0:
				x = 26
				N -= 26
			N = N // 26
			s = chr(x - 1 + ord('A')) + s
		return s


class Solution:

	def ExcelColumn(self, N):
		k = 65
		s = {}
		for i in range(1, 26):
			s[i] = chr(k)
			k += 1
		t = ''
		while N != 0:
			d = N % 26
			if d == 0:
				t += 'Z'
				N = (N - 1) // 26
			else:
				t += s[d]
				N = N // 26
		return t[::-1]