Given a positive integer, return its corresponding column title as appear in an Excel sheet.
Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB ….. etc. In other words, column 1 is named as “A”, column 2 as “B”, column 27 as “AA” and so on.
Example 1:
Input:
N = 28
Output: AB
Explanation: 1 to 26 are A to Z.
Then, 27 is AA and 28 = AB.
Ã¢â¬â¹Example 2:
Input: 
N = 13
Output: M
Explanation: M is the 13th character of
alphabet.
Your Task:
You don't need to read input or print anything. Your task is to complete the function colName() which takes the column number N as input and returns the column name represented as a string.
 
Expected Time Complexity: O(LogN).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 4294967295

Difficulty: MEDIUM
Tags: String algorithms

Data structures

Mathematics
Skill types: Data structures
Expected Time Complexity: O(LogN).
Expected Auxiliary Space: O(1).

Starter code:
#User function Template for python3

class Solution:
    def colName (self, n):
        # your code here

Solutions:
class Solution:

	def colName(self, n):
		str1 = ''
		while n != 0:
			if n % 26 == 0:
				str1 = 'Z' + str1
				n = (n - 1) // 26
			else:
				str1 = chr(n % 26 - 1 + 65) + str1
				n = n // 26
		return str1


import string

class Solution:

	def colName(self, n):
		asc = list(string.ascii_uppercase)
		out = ''
		while n > 0:
			inx = (n - 1) % 26 + 1
			out += asc[inx - 1]
			n = (n - 1) // 26
		return out[::-1]


class Solution:

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


class Solution:

	def colName(self, n):
		dic = {}
		dic[0] = 'Z'
		for i in range(65, 65 + 26):
			dic[i - 64] = chr(i)
		st = []
		while n > 0:
			curr = n % 26
			if curr == 0:
				curr = 26
				n -= 1
			st.append(chr(curr + 64))
			n //= 26
		st = st[::-1]
		return ''.join(st)


class Solution:

	def colName(self, n):
		res = ''
		while n > 0:
			res = chr(65 + (n - 1) % 26) + res
			n = (n - 1) // 26
		return res


class Solution:

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


class Solution:

	def colName(self, n):
		columns = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		string = ''
		while n > 0:
			(n, idx) = divmod(n, 26)
			if idx == 0:
				n -= 1
			string = columns[idx - 1] + string
		return string


class Solution:

	def colName(self, n):
		ans = []
		while n:
			n -= 1
			ans.append(chr(n % 26 + ord('A')))
			n //= 26
		return ''.join(reversed(ans))


class Solution:

	def colName(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 colName(self, n):
		s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		ans = ''
		while n:
			ans = s[(n - 1) % 26] + ans
			n = (n - 1) // 26
		return ans


class Solution:

	def colName(self, n):
		n = n - 1
		ans = ''
		i = 1
		while n >= pow(26, i):
			n -= pow(26, i)
			i += 1
		x = i - 1
		while x >= 0:
			ans += chr(n // pow(26, x) + 65)
			n = n % pow(26, x)
			x -= 1
		return ans


class Solution:

	def colName(self, n):
		res = n
		str_val = ''
		while res > 0:
			rem = res % 26
			if rem == 0:
				str_val = 'Z' + str_val
				res = res // 26 - 1
			else:
				str_val = chr(rem - 1 + ord('A')) + str_val
				res = res // 26
		return str_val.upper()


class Solution:

	def colName(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
		return s[::-1]


class Solution:

	def colName(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 colName(self, n):
		ans = ''
		while n:
			ans += chr(ord('A') + (n - 1) % 26)
			n = (n - 1) // 26
		return ans[::-1]


class Solution:

	def colName(self, n):
		ans = ''
		while n > 0:
			if n % 26 == 0:
				ans = 'Z' + ans
				n = n // 26 - 1
			else:
				ans = chr(64 + n % 26) + ans
				n = n // 26
		return ans


class Solution:

	def colName(self, n):
		out = ''
		while n > 0:
			mod = n % 26
			n = n // 26
			if mod == 0:
				n -= 1
				mod = 26
			out = chr(mod + ord('A') - 1) + out
		return out


class Solution:

	def colName(self, n):
		ans = []
		while n > 0:
			(n, r) = divmod(n, 26)
			if r == 0:
				r = 26
				n -= 1
			ans.append(chr(64 + r))
		return ''.join(reversed(ans))


class Solution:

	def colName(self, n):
		N = n
		ans = ''
		while N:
			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 colName(self, n):
		res = ''
		while n >= 1:
			x = n % 26
			if x >= 1:
				res += chr(int(x) + 64)
				n = n / 26
			else:
				res += 'Z'
				n = n // 26 - 1
		res1 = ''
		for i in range(len(res) - 1, -1, -1):
			res1 += res[i]
		return res1


class Solution:

	def colName(self, n):
		chars = ' abcdefghijklmnopqrstuvwxyz'.upper()

		def recursive(n):
			nonlocal chars
			if n <= 26:
				return chars[n]
			div = n // 26
			rem = n % 26
			if rem == 0:
				return recursive(div - 1) + recursive(26)
			return recursive(div) + recursive(rem)
		return recursive(n)


class Solution:

	def colName(self, n):
		base = 26
		col = ''
		if n > 0 and n < 27:
			return chr(n + 64)
		while n > 0:
			x = n % 26
			if x == 0:
				col = 'Z' + col
				n = n // 26 - 1
			else:
				col = chr(x + 64) + col
				n = n // 26
		return col


class Solution:

	def colName(self, n):
		col_name = ''
		while n > 0:
			rem = (n - 1) % 26
			col_name += chr(ord('A') + rem)
			n = (n - 1) // 26
		return col_name[::-1]


class Solution:

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


class Solution:

	def colName(self, n):
		x = 'abcdefghijklmnopqrstuvwxyz'
		x = x.upper()
		ans = ''
		while n:
			ans += x[(n - 1) % 26]
			n = (n - 1) // 26
		return ans[::-1]


class Solution:

	def colName(self, n):
		name = ''
		while n > 0:
			(n, rem) = divmod(n - 1, 26)
			name = chr(rem + 65) + name
		return name


import math

class Solution:

	def colName(self, n):
		alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		d = {}
		for i in range(26):
			d[i] = alpha[i]
		ans = ''
		while n:
			n -= 1
			ans += d[n % 26]
			n -= n % 26
			n = n // 26
		return ans[::-1]


class Solution:

	def colName(self, n):
		res = ''
		while n != 0:
			digit = n % 26
			if digit == 0:
				digit = 26
				n = n // 26 - 1
			else:
				n = n // 26
			char = chr(ord('A') + digit - 1)
			res = char + res
		return res


class Solution:

	def colName(self, n):
		res = ''
		while n > 0:
			n -= 1
			temp = n % 26
			res += chr(ord('A') + temp)
			n //= 26
		res = res[::-1]
		return res


class Solution:

	def colName(self, n):
		s = ''
		while n:
			if n % 26 == 0:
				s = chr(90) + s
				n = (n - 1) // 26
			else:
				s = chr(n % 26 - 1 + 65) + s
				n = n // 26
		return s


class Solution:

	def colName(self, n):
		string = ''
		while n > 0:
			character = n % 26
			if character == 0:
				string = 'Z' + string
				n = n // 26 - 1
			else:
				character_to_be_appended = chr(65 + character - 1)
				string = character_to_be_appended + string
				n = n // 26
		return string


alpha = 'ZABCDEFGHIJKLMNOPQRSTUVWXY'

class Solution:

	def colName(self, n):
		alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		st = ''
		while n > 0:
			q = n % 26
			if q == 0:
				q = 26
			st += alpha[q - 1]
			n -= q
			n = n // 26
		return st[::-1]


class Solution:

	def colName(self, n):
		ans = ''
		d = dict()
		d[0] = 'Z'
		for i in range(1, 26):
			d[i] = chr(ord('A') + i - 1)
		while n >= 26:
			a = int(n % 26)
			ans += d[a]
			n = int(n / 26)
			if a == 0:
				n -= 1
		ans += d[n % 26]
		return ans[::-1]


class Solution:

	def colName(self, n):
		l = [chr(x) for x in range(ord('A'), ord('Z') + 1)]
		s = ''
		while n > 0:
			s += l[(n - 1) % 26]
			n = (n - 1) // 26
		return s[::-1]


class Solution:

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


class Solution:

	def colName(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 colName(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 colName(self, n):
		master = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		if n < 26:
			return master[n - 1]
		else:
			result = ''
			if n % 26 == 0:
				if n // 26 == 1:
					return master[n % 26 - 1]
				else:
					return self.colName(n // 26 - 1) + master[n % 26 - 1]
			else:
				return self.colName(n // 26) + master[n % 26 - 1]


class Solution:

	def colName(self, 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 colName(self, n):
		l = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		k = True
		s = ''
		g = 0
		while n:
			if n % 26 == 0:
				s = 'Z' + s
				n = (n - 1) // 26
			else:
				s = chr(n % 26 - 1 + 65) + s
				n = n // 26
		return s


class Solution:

	def colName(self, n):
		st = ''
		while n > 0:
			a = n % 26
			if a == 0:
				st = 'Z' + st
				n //= 26
				n -= 1
			else:
				st = chr(ord('A') + a - 1) + st
				n //= 26
		return st


class Solution:

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


class Solution:

	def colName(self, n):
		l = [-1, '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:
			if n % 26 == 0:
				s = l[-1] + s
				n = (n - 1) // 26
			else:
				s = l[n % 26] + s
				n = n // 26
		return s


class Solution:

	def colName(self, n):
		res = ''
		while n:
			c = chr(ord('A') + (n - 1) % 26)
			res = c + res
			n = (n - 1) // 26
		return res


class Solution:

	def colName(self, n):
		stir = []
		while n > 0:
			rem = n % 26
			if rem == 0:
				stir.append('Z')
				n = (n - 1) // 26
			else:
				value = rem - 1
				stir.append(chr(value + ord('A')))
				n = n // 26
		return ''.join(reversed(stir))


class Solution:

	def colName(self, n):
		import string as s
		a = list(s.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.colName(self, q - 1) + a[r - 1]
			else:
				return Solution.colName(self, q) + a[r - 1]


class Solution:

	def colName(self, n):
		ans = ''
		while n:
			a = n % 26
			n = n // 26
			if a == 0:
				ans += 'Z'
				n -= 1
			else:
				ans += chr(65 + a - 1)
		return ans[::-1]


class Solution:

	def colName(self, n):
		res = ''
		while n > 0:
			i = n % 26
			if i == 0:
				res = 'Z' + res
				n = (n - 1) // 26
			else:
				res = chr(64 + i) + res
				n = n // 26
		return res


class Solution:

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


class Solution:

	def colName(self, n):
		res = ''
		while n != 0:
			letter = n % 26
			n //= 26
			if letter == 0:
				letter = 26
				n -= 1
			res = chr(96 + letter).upper() + res
		return res


class Solution:

	def colName(self, n):

		def mama(k):
			if k > 26:
				if k % 26 == 0:
					l = k // 26 - 1
					m = 26
				else:
					l = k // 26
					m = k % 26
				return mama(l) + chr(64 + m)
			else:
				if k % 26 == 0:
					l = 26
				else:
					l = k % 26
				return '' + chr(64 + l)


class Solution:

	def colName(self, n):
		s = ''
		while n > 0:
			a = n % 26
			if a == 0:
				s = 'Z' + s
				n = n // 26 - 1
			else:
				s = chr(a + 64) + s
				n = n // 26
		return s


class Solution:

	def colName(self, n):
		candidates = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		num_candidates = len(candidates)
		reversed_ans = ''
		while n:
			reversed_ans += candidates[(n - 1) % num_candidates]
			n = (n - 1) // num_candidates
		return reversed_ans[::-1]


class Solution:

	def colName(self, n):
		res = ''
		while n > 0:
			if n % 26 == 0:
				res += 'Z'
				n = (n - 1) // 26
			else:
				res = res + chr(n % 26 + 65 - 1)
				n = n // 26
		final = res[::-1]
		return final


class Solution:

	def colName(self, n):
		s = {}
		k = 65
		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]


class Solution:

	def colName(self, n):
		arr = [chr(i) for i in range(65, 91)]
		q = n // 26
		r = n % 26
		s = ''
		s = arr[r - 1]
		while q > 0:
			temp = q
			q = temp // 26
			r = temp % 26
			if s[0] == 'Z':
				if r == 0:
					s = 'Z' + s
				else:
					s = arr[r - 2] + s
			elif r == 0:
				s = 'Z' + s
			else:
				s = arr[r - 1] + s
		return s


alpha = 'ZABCDEFGHIJKLMNOPQRSTUVWXY'

class Solution:

	def colName(self, n):
		if n < 26:
			return alpha[n]
		else:
			(q, r) = (n // 26, n % 26)
			if r == 0:
				if q == 1:
					return alpha[r]
				else:
					return self.colName(q - 1) + alpha[r]
			else:
				return self.colName(q) + alpha[r]