Given an integer N.Create a string using only lowercase characters from a to z that follows the given rules.
When N is even:
Use N/2 characters from the beginning of a-z and N/2 characters from the ending of a-z.
When N is greater than 26,continue repeating the instructions until length of string becomes N.
When N is odd:
Case 1: If the sum of digits of N is even, Select (N+1)/2 characters from the beginning of a-z and (N-1)/2 characters from the ending of a-z.
Case 2: If the sum of digits of N is odd, Select (N-1)/2 characters from the beginning of a-z and (N+1)/2 characters from the ending of a-z.
When N is greater than 26,continue repeating the instructions until length of string becomes N.
 
Example 1:
Input: 
N=21
Output: 
abcdefghijpqrstuvwxyz
Explanation:
Since 21 is odd and sum of digits
of 21 is also odd,we take (21-1)/2=10 
characters from the beginning and 
(21+1)/2=11 characters from the 
end of a-z.
Example 2: 
Input:
N=28 
Output: 
abcdefghijklmnopqrstuvwxyzaz
Explanation: 
Since 28>26, we keep repeating 
the process until length of string becomes 
28.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function BalancedString() which takes the integer N as input parameter and returns the string created using given procedures.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Implementation

Starter code: #User function Template for python3



class Solution:

    def BalancedString(self,N):

        #code here

    

Solutions:
class Solution:

	def BalancedString(self, N):
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		i = N % 26 // 2
		output = N // 26 * alpha
		output += alpha[:i]
		if N % 2:
			check = sum(map(int, [*str(N)])) % 2
			if check:
				output += alpha[-i - 1]
			else:
				output += alpha[i]
		if not i:
			return output
		output += alpha[-i:]
		return output


class Solution:

	def BalancedString(self, N):
		ans = []
		N_sum = 0
		temp = N
		while temp:
			N_sum += temp % 10
			temp //= 10
		while N >= 26:
			for i in range(26):
				ans.append(chr(i + 97))
			N -= 26
		if N % 2 == 0:
			upper_left = N // 2
			lower_right = 26 - upper_left
		elif N_sum % 2 == 0:
			upper_left = (N + 1) // 2
			lower_right = 26 - upper_left + 1
		else:
			upper_left = (N - 1) // 2
			lower_right = 26 - upper_left - 1
		for x in range(upper_left):
			ans.append(chr(x + 97))
		for x in range(lower_right, 26):
			ans.append(chr(x + 97))
		return ''.join(ans)


class Solution:

	def BalancedString(self, N):
		res = ''
		temp = 'abcdefghijklmnopqrstuvwxyz'
		if N % 2 == 0:
			for i in range(0, N, 26):
				if N - i >= 26:
					res += temp
				else:
					ml = (N - i) // 2
					mr = (N - i) // 2
					res += temp[0:ml]
					res += temp[26 - mr:]
		else:
			sd = 0
			ns = str(N)
			for i in range(len(ns)):
				sd += int(ns[i])
			if sd % 2 == 0:
				for i in range(0, N, 26):
					if N - i >= 26:
						res += temp
					else:
						ml = (N - i) // 2
						mr = (N - i) // 2
						res += temp[0:ml + 1]
						res += temp[26 - mr:]
			else:
				for i in range(0, N, 26):
					if N - i >= 26:
						res += temp
					else:
						ml = (N - i) // 2
						mr = (N - i) // 2
						res += temp[0:ml]
						res += temp[26 - mr - 1:]
		return res


class Solution:

	def BalancedString(self, n):
		s = 'abcdefghijklmnopqrstuvwxyz'
		s1 = str(n)
		k = 0
		q = 0
		for i in range(len(s1)):
			k += int(s1[i])
		if k % 2 == 0:
			q = 1
		i = 0
		o = ''
		k = n // 26
		for i in range(k):
			o += s
		k = n % 26
		if k % 2 == 0:
			k = k // 2
			o += s[:k] + s[26 - k:]
		elif q == 1:
			k = k // 2
			o += s[:k + 1] + s[26 - k:]
		else:
			k = k // 2
			o += s[:k] + s[26 - k - 1:]
		return o


class Solution:

	def __init__(self):
		self._ALPHABETS = 'abcdefghijklmnopqrstuvwxyz'

	def _sum_of_digits(self, num):
		res = 0
		while num:
			res += num % 10
			num //= 10
		return res

	def BalancedString(self, N):
		res = [self._ALPHABETS * (N // 26), None, None]
		(og_N, N) = (N, N % 26)
		if not N:
			return res[0]
		count = (N >> 1) + (og_N & 1) * (1 - (self._sum_of_digits(og_N) & 1))
		res[1] = self._ALPHABETS[:count]
		res[2] = self._ALPHABETS[26 - N + count:]
		return ''.join(res)


class Solution:

	def BalancedString(self, N):
		s1 = 'abcdefghijklmnopqrstuvwxyz'
		ans = ''
		temp = N
		sum = 0
		while temp != 0:
			sum += temp % 10
			temp = temp // 10
		if N > 26:
			while N > 26:
				ans += s1
				N = N - 26
		if N % 2 == 0:
			ans += s1[0:N // 2]
			ans += s1[26 - N // 2:]
		if N % 2 != 0:
			if sum % 2 == 0:
				ans += s1[0:(N + 1) // 2]
				ans += s1[26 - (N - 1) // 2:]
			else:
				ans += s1[0:(N - 1) // 2]
				ans += s1[26 - (N + 1) // 2:]
		return ans


class Solution:

	def BalancedString(self, N):
		number = N
		string = 'abcdefghijklmnopqrstuvwxyz'
		output_str = ''
		if N >= 26:
			output_str = string * (N // 26)
			number = N % 26
		if N % 2 == 0:
			start_char = int(number / 2)
			output_str = output_str + string[:start_char] + string[26 - start_char:]
		else:
			sum_digit = 0
			for char in str(N):
				sum_digit = sum_digit + int(char)
			if sum_digit % 2 == 0:
				start_char = int((number + 1) / 2)
				start_char1 = int((number - 1) / 2)
			else:
				start_char = int((number - 1) / 2)
				start_char1 = int((number + 1) / 2)
			output_str = output_str + string[:start_char] + string[26 - start_char1:]
		return output_str


class Solution:

	def BalancedString(self, N):
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		string = ''
		if N % 2 == 0:
			times = N // 26
			remain = N - times * 26
			p = 26
			result = alpha * times + alpha[:(remain + 1) // 2] + alpha[-1:-((remain - 1) // 2 + 1) - 1:-1][::-1]
			return result
		else:
			sum1 = sum(map(int, list(str(N))))
			if sum1 % 2 == 0:
				times = N // 26
				rem = N - times * 26
				p = 26
				result = alpha * times + alpha[:(rem + 1) // 2] + alpha[-1:-((rem - 1) // 2) - 1:-1][::-1]
				return result
			else:
				times = N // 26
				rem = N - times * 26
				p = 26
				result = alpha * times + alpha[:(rem - 1) // 2] + alpha[-1:-((rem + 1) // 2) - 1:-1][::-1]
				return result


class Solution:

	def BalancedString(self, N):
		sod = 0
		s = str(N)
		for c in s:
			sod += int(c)
		alpha = [chr(i) for i in range(97, 123)]
		rev = alpha[::-1]
		(q, N) = divmod(N, 26)
		ans = q * ''.join(alpha)
		if N == 0:
			return ans
		if N & 1:
			if sod % 2 != 0:
				first = (N - 1) // 2
				second = (N + 1) // 2
			else:
				second = (N - 1) // 2
				first = (N + 1) // 2
		else:
			first = second = N // 2
		ans += ''.join(alpha[:first]) + ''.join(rev[:second][::-1])
		return ans


class Solution:

	def BalancedString(self, N):
		Alp = 'abcdefghijklmnopqrstuvwxyz'
		ReAlp = Alp[::-1]
		s = ''
		if N >= 26:
			s += Alp * (N // 26)
		if N % 2 == 0:
			N = N % 26
			j = ReAlp[0:N // 2]
			s = s + Alp[0:N // 2] + j[::-1]
			return s
		else:
			k = N
			jk = 0
			while k > 0:
				jk += k % 10
				k = k // 10
			if jk % 2 == 0:
				N = N % 26
				j = ReAlp[:(N - 1) // 2]
				s = s + Alp[:(N + 1) // 2] + j[::-1]
				return s
			N = N % 26
			j = ReAlp[:(N + 1) // 2]
			s = s + Alp[:(N - 1) // 2] + j[::-1]
			return s


class Solution:

	def SumOfDigits(self, N):
		res = 0
		while N > 0:
			res += N % 10
			N = N // 10
		return res

	def BalancedString(self, N):
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		res = [alpha * (N // 26)]
		originalN = N
		N = N % 26
		if N == 0:
			return res[0]
		count = N // 2 + N % 2 * (1 - self.SumOfDigits(originalN) % 2)
		res.append(alpha[:count])
		res.append(alpha[26 - (N - count):])
		return ''.join(res)


class Solution:

	def BalancedString(self, N):
		stri = 'abcdefghijklmnopqrstuvwxyz'
		res = ''
		count = N // 26
		rem = N % 26
		res = stri * count
		if N % 2 == 0:
			res += stri[:rem // 2] + stri[26 - rem // 2:]
			return res
		else:
			smu = 0
			for i in str(N):
				smu += int(i)
			if smu % 2 == 0:
				res += stri[:(rem + 1) // 2] + stri[26 - (rem - 1) // 2:]
			else:
				res += stri[:(rem - 1) // 2] + stri[26 - (rem + 1) // 2:]
			return res


import math

class Solution:

	def BalancedString(self, N):
		string = []
		times = math.floor(N / 26)
		for i in range(times):
			string += 'abcdefghijklmnopqrstuvwxyz'
		rem = N % 26
		start = ord('a')
		end = ord('z')
		if N % 2 == 0:
			for i in range(rem // 2):
				string += chr(start)
				start += 1
			temp = ''
			for i in range(rem // 2):
				temp = chr(end) + temp
				end -= 1
			string += temp
		else:
			sumofdig = 0
			for val in str(N):
				sumofdig += int(val)
			if sumofdig % 2 == 0:
				for i in range((rem + 1) // 2):
					string += chr(start)
					start += 1
				temp = ''
				for i in range((rem - 1) // 2):
					temp = chr(end) + temp
					end -= 1
				string += temp
			else:
				for i in range((rem - 1) // 2):
					string += chr(start)
					start += 1
				temp = ''
				for i in range((rem + 1) // 2):
					temp = chr(end) + temp
					end -= 1
				string += temp
		result = ''
		for val in string:
			result += val
		return result


class Solution:

	def BalancedString(self, n):
		st = 'abcdefghijklmnopqrstuvwxyz'
		s = ''
		if n % 2 == 0:
			while n > 26:
				s += st
				n -= 26
			s += st[:n // 2] + st[-(n // 2):]
			return s
		else:
			ns = sum([int(x) for x in str(n)])
			if ns % 2 != 0:
				while n >= 26:
					s += st
					n -= 26
				s += st[:(n - 1) // 2] + st[-((n + 1) // 2):]
				return s
			else:
				while n >= 26:
					s += st
					n -= 26
				if n == 1:
					s += st[:(n + 1) // 2]
				else:
					s += st[:(n + 1) // 2] + st[-((n - 1) // 2):]
				return s


class Solution:

	def digitSum(self, num):
		s = 0
		while num:
			s += num % 10
			num = num // 10
		return s

	def BalancedString(self, N):
		str = 'abcdefghijklmnopqrstuvwxyz'
		num = N // 26
		ans = ''
		for i in range(num):
			ans += str
		num = N % 26
		if num % 2 == 0:
			ans += str[:num // 2]
			ans += str[26 - num // 2:26]
		elif Solution.digitSum(self, N) % 2 == 0:
			ans += str[:num // 2 + 1]
			ans += str[26 - num // 2:26]
		else:
			ans += str[:num // 2]
			ans += str[26 - num // 2 - 1:26]
		return ans


class Solution:

	def BalancedString(self, N):
		chrs = ''
		for i in range(26):
			chrs += chr(ord('a') + i)
		res = ''
		if N % 2 == 0:
			f = N // 26
			res += chrs * f
			newN = N - f * 26
			front = newN // 2
			end = newN // 2
		else:
			val = N
			digitsum = 0
			while val > 0:
				digitsum += val % 10
				val //= 10
			if digitsum % 2 == 0:
				f = N // 26
				res += chrs * f
				newN = N - f * 26
				front = (newN + 1) // 2
				end = (newN - 1) // 2
			else:
				f = N // 26
				res += chrs * f
				newN = N - f * 26
				front = (newN - 1) // 2
				end = (newN + 1) // 2
		s = 0
		l = 26 - end
		while front > 0:
			res += chrs[s]
			s += 1
			front -= 1
		while end > 0:
			res += chrs[l]
			l += 1
			end -= 1
		return res


import string

class Solution:

	def BalancedString(self, N):
		lst = ''
		for i in range(97, 123):
			lst += chr(i)
		res = ''
		d = list(str(N))
		d = list(map(int, d))
		if N > 26:
			v = N // 26
			res += lst * v
			N = N % 26
		if N % 2 == 0:
			v = N // 2
			res += lst[0:v]
			res += lst[::-1][0:v][::-1]
			return res
		elif len(d) >= 2 and sum(d) % 2 == 0:
			v = (N + 1) // 2
			res += lst[0:v]
			t = (N - 1) // 2
			res += lst[::-1][:t][::-1]
			return res
		else:
			v = (N - 1) // 2
			res += lst[0:v]
			t = (N + 1) // 2
			res += lst[::-1][0:t][::-1]
			return res


class Solution:

	def BalancedString(self, N):
		ans = ''
		full = 'abcdefghijklmnopqrstuvwxyz'
		a = N // 26
		b = N % 26
		for _ in range(a):
			ans = ans + full
		if N % 2 == 0:
			for i in range(b // 2):
				ans = ans + full[i]
			for i in range(26 - b // 2, 26):
				ans = ans + full[i]
		else:
			SumOfDigits = 0
			temp = N
			while temp != 0:
				SumOfDigits = SumOfDigits + temp % 10
				temp = temp // 10
			if SumOfDigits % 2:
				for i in range((b - 1) // 2):
					ans = ans + full[i]
				for i in range(26 - (b + 1) // 2, 26):
					ans = ans + full[i]
			else:
				for i in range((b + 1) // 2):
					ans = ans + full[i]
				for i in range(26 - (b - 1) // 2, 26):
					ans = ans + full[i]
		return ans


class Solution:

	def BalancedString(self, N):
		li = 'abcdefghijklmnopqrstuvwxyz'
		n = N // 26
		p = N
		ans = li * n
		c = 0
		while N > 0:
			c += N % 10
			N = N // 10
		N = p % 26
		if c % 2 == 0:
			if N % 2 == 0:
				ans += li[:N // 2] + li[26 - N // 2:26]
			else:
				ans += li[:(N + 1) // 2] + li[26 - (N - 1) // 2:26]
		elif N % 2 == 0:
			ans += li[:N // 2] + li[26 - N // 2:26]
		else:
			ans += li[:(N - 1) // 2] + li[26 - (N + 1) // 2:26]
		return ans


class Solution:

	def BalancedString(self, N):
		letters = ''.join([chr(c) for c in range(ord('a'), ord('z') + 1)])
		c0 = N // 26
		c1 = N % 26
		if c1 == 0:
			return letters * c0
		if c1 % 2 == 0:
			left = right = c1 // 2
		else:
			c2 = 0
			c3 = N
			while c3:
				c2 += c3 % 10
				c3 //= 10
			if c2 % 2 == 0:
				left = (c1 + 1) // 2
				right = (c1 - 1) // 2
			else:
				left = (c1 - 1) // 2
				right = (c1 + 1) // 2
		return letters * c0 + letters[:left] + letters[26 - right:26]


class Solution:

	def BalancedString(self, N):
		st = ''
		ans = ''
		for c in range(97, 123):
			st += chr(c)
		a = N // 26
		b = N % 26
		while a:
			ans += st
			a -= 1
		if N % 2 == 0:
			for i in range(0, b // 2):
				ans += chr(97 + i)
			for i in range(26 - b // 2, 26):
				ans += chr(97 + i)
		else:
			sod = 0
			t = N
			while t:
				sod += t % 10
				t = t // 10
			if sod % 2 == 0:
				for i in range(0, (b + 1) // 2):
					ans += chr(97 + i)
				for i in range(26 - b // 2, 26):
					ans += chr(97 + i)
			else:
				for i in range(0, b // 2):
					ans += chr(97 + i)
				for i in range(26 - (b + 1) // 2, 26):
					ans += chr(97 + i)
		return ans


class Solution:

	def checkGrater(self, N):
		return (int(N / 26), N % 26)

	def isSumEven(self, number):
		number_result = 0
		for s in str(number):
			number_result += int(s)
		if number_result % 2 == 0:
			return True
		else:
			return False

	def BalancedString(self, N):
		alphabet = 'abcdefghijklmnopqrstuvwxyz'
		result = ''
		(repeat, last) = self.checkGrater(N)
		for i in range(repeat):
			result += alphabet
		if last % 2 == 0:
			my_index = int(last / 2)
			result += alphabet[:my_index]
			result += alphabet[26 - my_index:]
		else:
			start_index = 0
			end_index = 0
			if self.isSumEven(N):
				start_index = int((last + 1) / 2)
				end_index = int((last - 1) / 2)
			else:
				start_index = int((last - 1) / 2)
				end_index = int((last + 1) / 2)
			result += alphabet[:start_index]
			result += alphabet[26 - end_index:]
		return result


class Solution:

	def BalancedString(self, n):
		ans = ''
		if n % 2 == 0:
			q = n // 26
			r = n % 26
			for j in range(q):
				for i in range(97, 97 + 26 // 2):
					ans += chr(i)
				for i in range(122 - 26 // 2 + 1, 123):
					ans += chr(i)
			for i in range(97, 97 + r // 2):
				ans += chr(i)
			for i in range(122 - r // 2 + 1, 123):
				ans += chr(i)
		else:
			t = 0
			s = str(n)
			for i in s:
				t += int(i)
			q = n // 26
			r = n % 26
			if t % 2 == 0:
				for j in range(q):
					for i in range(97, 97 + (26 + 1) // 2 + 1):
						ans += chr(i)
					for i in range(122 - (26 - 1) // 2 + 1, 123):
						ans += chr(i)
				for i in range(97, 97 + (r - 1) // 2 + 1):
					ans += chr(i)
				for i in range(122 - (r + 1) // 2 + 2, 123):
					ans += chr(i)
			else:
				for j in range(q):
					for i in range(97, 97 + (26 - 1) // 2 + 1):
						ans += chr(i)
					for i in range(122 - (26 + 1) // 2 + 1, 123):
						ans += chr(i)
				for i in range(97, 97 + (r - 1) // 2):
					ans += chr(i)
				for i in range(122 - (r + 1) // 2 + 1, 123):
					ans += chr(i)
		return ans


class Solution:

	def BalancedString(self, N):
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		reverse = alpha[::-1]
		(start, end) = (0, 0)
		ret = ''
		(q, r) = divmod(N, 26)
		ret += alpha * q
		temp = 0
		for digit in str(N):
			temp += int(digit)
		if r & 1:
			if temp % 2 != 0:
				start = (r - 1) // 2
				end = (r + 1) // 2
			else:
				start = (r + 1) // 2
				end = (r - 1) // 2
		else:
			start = end = r // 2
		ret += ''.join(alpha[:start]) + ''.join(reverse[:end][::-1])
		return ret


class Solution:

	def BalancedString(self, N):
		x = N
		string = ''
		alphabet = ['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']
		while len(string) < N:
			if x % 2 == 0:
				for i in range(min(13, int(x / 2))):
					string += alphabet[i]
				for i in range(min(13, int(x / 2))):
					string += alphabet[int(26 - min(13, int(x / 2)) + i)]
				x -= 26
			elif sum(list(map(int, str(N)))) % 2 == 0:
				for i in range(min(13, int((x + 1) / 2))):
					string += alphabet[i]
				for i in range(min(13, int((x - 1) / 2))):
					string += alphabet[int(-min(13, (x - 1) / 2) + i)]
				x -= 26
			else:
				for i in range(min(13, int((x - 1) / 2))):
					string += alphabet[i]
				for i in range(min(13, int((x + 1) / 2))):
					string += alphabet[int(-min(13, (x + 1) / 2) + i)]
				x -= 26
		return string


import string

class Solution:

	def BalancedString(self, N):
		n = str(N)
		summ = 0
		for i in n:
			summ = summ + int(i)
		strr = ''
		if N > 26:
			rep = N // 26
			for i in range(rep):
				for i in string.ascii_lowercase:
					strr = strr + i
			N = N % 26
		if N ^ 1 == N + 1:
			f = N // 2
			l = N // 2
		elif summ ^ 1 == summ + 1:
			f = (N + 1) // 2
			l = (N - 1) // 2
		else:
			f = (N - 1) // 2
			l = (N + 1) // 2
		for i in range(f):
			strr = strr + string.ascii_lowercase[i]
		for i in range(26 - l, 26):
			strr = strr + string.ascii_lowercase[i]
		return strr


class Solution:

	def BalancedString(self, N):
		strN = str(N)
		s = 'abcdefghijklmnopqrstuvwxyz'
		ans = ''
		if N // 26 >= 1:
			ans = s * (N // 26)
			N = N % 26
		revs = s[::-1]
		if N % 2 == 0:
			return ans + s[:N // 2] + revs[:N // 2][::-1]
		elif sum([int(x) for x in strN]) % 2 == 0:
			return ans + s[:(N + 1) // 2] + revs[:(N - 1) // 2][::-1]
		else:
			return ans + s[:(N - 1) // 2] + revs[:(N + 1) // 2][::-1]


class Solution:

	def BalancedString(self, N):
		str = 'abcdefghijklmnopqrstuvwxyz'
		res = [str * (N // 26), None, None]
		(tempN, N) = (N, N % 26)
		if not N:
			return res[0]
		count = (N >> 1) + (tempN & 1) * (1 - (self.sumOfDigit(tempN) & 1))
		res[1] = str[:count]
		res[2] = str[26 - N + count:]
		return ''.join(res)

	def sumOfDigit(self, num):
		res = 0
		while num:
			res += num % 10
			num //= 10
		return res


class Solution:

	def BalancedString(self, N):
		st = 'abcdefghijklmnopqrstuvwxyz'
		s = ''
		n = N
		while N > 26:
			s = s + st
			N = N - 26
		if N % 2 == 0:
			s = s + st[:N // 2]
			s = s + st[26 - N // 2:]
		else:
			sum = 0
			while n > 0:
				sum = sum + n % 10
				n = n // 10
			if sum % 2 == 0:
				s = s + st[:(N + 1) // 2]
				s = s + st[26 - (N - 1) // 2:]
			else:
				s = s + st[:(N - 1) // 2]
				s = s + st[26 - (N + 1) // 2:]
		return s


class Solution:

	def BalancedString(self, N):
		if N & 1 == 0:
			if N > 26:
				s = 'abcdefghijklmnopqrstuvwxyz' * (N // 26)
				N = N % 26
				N = N // 2
			else:
				N = N // 2
				s = ''
			for i in range(N):
				s = s + chr(97 + i)
			for i in range(N):
				s = s + chr(123 - N + i)
		else:
			sum1 = 0
			i = N
			while i > 0:
				sum1 = sum1 + i % 10
				i = i // 10
			if N > 26:
				s = 'abcdefghijklmnopqrstuvwxyz' * (N // 26)
				N = N % 26
			else:
				s = ''
			if sum1 & 1 == 0:
				for i in range((N + 1) // 2):
					s = s + chr(97 + i)
				for i in range((N - 1) // 2):
					s = s + chr(123 - (N - 1) // 2 + i)
			else:
				for i in range((N - 1) // 2):
					s = s + chr(97 + i)
				for i in range((N + 1) // 2):
					s = s + chr(123 - (N + 1) // 2 + i)
		return s


class Solution:

	def BalancedString(self, n):
		a = 'abcdefghijklmnopqrstuvwxyz'

		def sod(n):
			p = 0
			while n > 0:
				k = n % 10
				n = n // 10
				p += k
			return p
		ans = ''
		if n % 2 == 0:
			k = n // 26
			ans = a * k
			rem = n - 26 * k
			l = rem // 2
			r = l
			ans = ans + a[:l] + a[len(a) - r:]
		elif sod(n) % 2 == 0:
			k = n // 26
			ans = a * k
			rem = n - 26 * k
			l = (rem + 1) // 2
			r = (rem - 1) // 2
			ans = ans + a[:l] + a[len(a) - r:]
		else:
			k = n // 26
			ans = a * k
			rem = n - 26 * k
			l = (rem - 1) // 2
			r = (rem + 1) // 2
			ans = ans + a[:l] + a[len(a) - r:]
		return ans


def mayor_o_menor(numero):
	salida = []
	if numero < 26:
		salida.append(0)
		salida.append(numero)
		return salida
	else:
		division = numero // 26
		resto = numero % 26
		salida.append(division)
		salida.append(resto)
		return salida

def par_o_impar(numero):
	if numero % 2 == 0:
		return 'par'
	else:
		return 'impar'

def es_par(numero, abecedario):
	division = int(numero / 2)
	if division == 0:
		return ''
	else:
		salida = abecedario[:division] + abecedario[-division:]
		return salida

def suma_numero(numero):
	solucion = []
	for caracter in str(numero):
		solucion.append(int(caracter))
	return sum(solucion)

def es_impar(N, numero, abecedario):
	suma = suma_numero(N)
	if par_o_impar(suma) == 'par':
		inicio = int((numero + 1) / 2)
		final = int((numero - 1) / 2)
		if inicio == 1:
			return 'a'
		else:
			salida = abecedario[:inicio] + abecedario[-final:]
			return salida
	else:
		inicio = int((numero + 1) / 2)
		final = int((numero - 1) / 2)
		salida = abecedario[:final] + abecedario[-inicio:]
		return salida

class Solution:

	def BalancedString(self, N):
		Abc = 'abcdefghijklmnopqrstuvwxyz'
		mom_salida = mayor_o_menor(N)
		if mom_salida[0] == 0:
			if par_o_impar(N) == 'par':
				return es_par(N, Abc)
			else:
				return es_impar(N, N, Abc)
		elif par_o_impar(N) == 'par':
			multiplicador = mom_salida[0]
			return multiplicador * Abc + es_par(mom_salida[1], Abc)
		else:
			multiplicador = mom_salida[0]
			return multiplicador * Abc + es_impar(N, mom_salida[1], Abc)


class Solution:

	def tell_sum(self, N):
		dig_sum = 0
		while N > 0:
			dig_sum += N % 10
			N = N // 10
			if N == 0:
				break
		return dig_sum

	def BalancedString(self, N):
		alphs = []
		for i in range(97, 97 + 26):
			alphs.append(chr(i))
		if N % 2 == 0:
			if N <= 26:
				ans = ''
				for i in range(N // 2):
					ans += alphs[i]
				for i in range(26 - N // 2, 26):
					ans += alphs[i]
			else:
				ans = ''.join(alphs) * (N // 26)
				mod = N % 26
				for i in range(mod // 2):
					ans += alphs[i]
				for i in range(26 - mod // 2, 26):
					ans += alphs[i]
		else:
			dig_sum = self.tell_sum(N)
			if N <= 26:
				if dig_sum % 2 == 0:
					ans = ''
					for i in range((N + 1) // 2):
						ans += alphs[i]
					for i in range(26 - (N - 1) // 2, 26):
						ans += alphs[i]
				else:
					ans = ''
					for i in range((N - 1) // 2):
						ans += alphs[i]
					for i in range(26 - (N + 1) // 2, 26):
						ans += alphs[i]
			else:
				ans = ''.join(alphs) * (N // 26)
				mod = N % 26
				if dig_sum % 2 == 0:
					for i in range((mod + 1) // 2):
						ans += alphs[i]
					for i in range(26 - (mod - 1) // 2, 26):
						ans += alphs[i]
				else:
					for i in range((mod - 1) // 2):
						ans += alphs[i]
					for i in range(26 - (mod + 1) // 2, 26):
						ans += alphs[i]
		return ans


class Solution:

	def BalancedString(self, N):
		temp = ''
		s = 'abcdefghijklmnopqrstuvwxyz'
		if N % 2 == 0:
			while N > 0:
				if N <= 26:
					temp += s[:N // 2]
					temp += s[26 - N // 2:]
				if N > 26:
					temp += s
				N -= 26
		else:
			n = 0
			K = N
			while K:
				i = K % 10
				K = K // 10
				n += i
			if n % 2 == 0:
				while N > 0:
					if N <= 26:
						temp += s[:(N + 1) // 2]
						temp += s[26 - (N - 1) // 2:]
					if N > 26:
						temp += s
					N -= 26
			else:
				k = True
				while N > 0:
					if N <= 26:
						temp += s[:(N - 1) // 2]
						temp += s[26 - (N + 1) // 2:]
					if N > 26:
						temp += s
					N -= 26
		return temp


import string
import math

class Solution:

	def __init__(self):
		self.chars = list(string.ascii_lowercase)

	def BalancedString(self, N):
		builder = None
		res = ''
		if N % 2 == 0:
			builder = self.even_builder
		elif self.sum_digits(N) % 2 == 0:
			builder = self.odd_even_builder
		else:
			builder = self.odd_odd_builder
		res = ''.join(self.chars * (math.ceil(N / 26) - 1))
		res = res + builder(N - len(res))
		return res

	def even_builder(self, N):
		mid = int(N / 2)
		return ''.join(self.chars[:mid] + self.chars[-mid:])

	def odd_even_builder(self, N):
		if N == 1:
			return self.chars[0]
		start = int((N + 1) / 2)
		end = int((N - 1) / 2)
		return ''.join(self.chars[:start] + self.chars[-end:])

	def odd_odd_builder(self, N):
		if N == 1:
			return self.chars[-1]
		start = int((N - 1) / 2)
		end = int((N + 1) / 2)
		return ''.join(self.chars[:start] + self.chars[-end:])

	def sum_digits(self, N):
		sum = 0
		for digit in str(N):
			sum += int(digit)
		return sum


class Solution:

	def BalancedString(self, N):
		s = 'abcdefghijklmnopqrstuvwxyz'
		if N % 26 == 0:
			return N // 26 * s
		n = N
		res = ''
		q = N // 26
		if q >= 1:
			res = s * q
		if N % 2 == 0:
			if N > 26:
				r = N % 26
				res = res + s[:r // 2] + s[-(r // 2):]
			else:
				res = res + s[:N // 2] + s[-(N // 2):]
		else:
			Sum = 0
			while n > 0:
				rem = n % 10
				Sum += rem
				n = n // 10
			if N > 26:
				r = N % 26
				if Sum % 2 == 0:
					if r == 1:
						res = res + s[r - 1]
					else:
						res = res + s[:(r + 1) // 2] + s[-(r - 1) // 2:]
				else:
					res = res + s[:(r - 1) // 2] + s[-(r + 1) // 2:]
			elif Sum % 2 == 0:
				res = res + s[:(N + 1) // 2] + s[-(N - 1) // 2:]
			else:
				res = res + s[:(N - 1) // 2] + s[-(N + 1) // 2:]
		return res


class Solution:

	def BalancedString(self, N):
		s = 'abcdefghijklmnopqrstuvwxyz'
		res = ''
		cnt = N // 26
		rem = N % 26
		res = s * cnt
		if N % 2 == 0:
			res += s[:rem // 2] + s[26 - rem // 2:]
			return res
		else:
			sm = 0
			for i in str(N):
				sm += int(i)
			if sm % 2 == 0:
				res += s[:(rem + 1) // 2] + s[26 - (rem - 1) // 2:]
				return res
			else:
				res += s[:(rem - 1) // 2] + s[26 - (rem + 1) // 2:]
				return res


class Solution:

	def BalancedString(self, N):
		N_u = N
		temp = []
		alphab = ''
		if N > 26:
			N_u = N % 26
			alphab = 'abcdefghijklmnopqrstuvwxyz' * (N // 26)
		if N % 2 == 0:
			for i in range(N_u // 2):
				temp.append(chr(97 + i))
			for i in range(N_u // 2, N_u):
				temp.append(chr(122 - N_u + 1 + i))
		elif sum(list((int(i) for i in str(N)))) % 2 == 0:
			for i in range((N_u + 1) // 2):
				temp.append(chr(97 + i))
			for i in range((N_u - 1) // 2 + 1, N_u):
				temp.append(chr(122 - N_u + 1 + i))
		else:
			for i in range((N_u - 1) // 2):
				temp.append(chr(97 + i))
			for i in range((N_u + 1) // 2 - 1, N_u):
				temp.append(chr(122 - N_u + 1 + i))
		return alphab + ''.join((str(i) for i in temp))


class Solution:

	def BalancedString(self, N):

		def count(n):
			cnt = 0
			while n > 0:
				cnt += n % 10
				n //= 10
			return cnt
		lower = ''.join([chr(i) for i in range(97, 123)])
		x = N // 26
		n = N % 26
		if n % 2 == 0:
			half = n // 2
		elif count(N) % 2 == 0:
			half = (n + 1) // 2
		else:
			half = (n - 1) // 2
		last = lower[:half] + lower[26 - (n - half):]
		return lower * x + last


class Solution:

	def BalancedString(self, N):
		r = 'abcdefghijklmnopqrstuvwxyz'
		res = ''
		l = list(str(N))
		l = list(map(int, l))
		if N > 26:
			v = N // 26
			res += r * v
			N = N % 26
		if N % 2 == 0:
			v = N // 2
			res += r[:v]
			res += r[::-1][:v][::-1]
			return res
		elif len(l) >= 2 and sum(l) % 2 == 0:
			v = (N + 1) // 2
			res += r[:v]
			t = (N - 1) // 2
			res += r[::-1][:t][::-1]
			return res
		else:
			v = (N - 1) // 2
			res += r[:v]
			t = (N + 1) // 2
			res += r[::-1][:t][::-1]
			return res


class Solution:

	def BalancedString(self, N: int) -> str:
		atoz = 'abcdefghijklmnopqrstuvwxyz'
		(d, r) = divmod(N, 26)
		res = atoz * d
		if r:
			if r % 2 == 0:
				l = r // 2
				res += atoz[:l]
				res += atoz[-l:]
			else:
				sum_dig = sum(list(map(int, str(N))))
				if sum_dig % 2 == 0:
					l = (r + 1) // 2
					res += atoz[:l]
					if l - 1:
						res += atoz[-l + 1:]
				else:
					l = (r - 1) // 2
					if l:
						res += atoz[:l]
					res += atoz[-l - 1:]
		return res


class Solution:

	def BalancedString(self, N):
		l = ['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 % 2 == 0:
			while N > 26:
				ans.extend(l[:])
				N -= 26
			ans.extend(l[:N // 2])
			if -N // 2 != 0:
				ans.extend(l[-N // 2:])
		else:
			n = N
			sumDig = 0
			while n != 0:
				sumDig += n % 10
				n = n // 10
			while N > 26:
				ans.extend(l[:])
				N -= 26
			if sumDig % 2 == 0:
				ans.extend(l[:(N + 1) // 2])
				if -(N - 1) // 2 != 0:
					ans.extend(l[-(N - 1) // 2:])
			else:
				ans.extend(l[:(N - 1) // 2])
				if -(N + 1) // 2 != 0:
					ans.extend(l[-(N + 1) // 2:])
		return ''.join(ans)


class Solution:

	def BalancedString(self, N):
		if N == 0:
			return ''
		a = 'abcdefghijklmnopqrstuvwxyz'
		return a * (N // 26) + a[0:N % 26 // 2 + N % 26 % 2 - N % 26 % 2 * sum((int(v) for v in str(N))) % 2] + a[26 - (N % 26 // 2 + N % 26 % 2 * sum((int(v) for v in str(N))) % 2):]


class Solution:

	def BalancedString(self, N):
		az = 'abcdefghijklmnopqrstuvwxyz'
		str1 = str(N)
		summ = 0
		for _ in str1:
			summ += int(_)
		if N >= 26 and N % 2 == 0:
			times = int(N / 26)
			newN = int(N % 26)
			count = int(newN / 2)
			if count == 0:
				new = az * times
			else:
				new = az * times + az[:count] + az[-count:]
			return new
		if N >= 26 and N % 2 != 0:
			times = int(N / 26)
			newN = int(N % 26)
			if summ % 2 == 0:
				countEvenFirst = int((newN + 1) / 2)
				countEvenLast = int((newN - 1) / 2)
				new = az * times + az[:countEvenFirst] + az[26 - countEvenLast:]
			if summ % 2 != 0:
				countEvenFirst = int((newN - 1) / 2)
				countEvenLast = int((newN + 1) / 2)
				new = az * times + az[:countEvenFirst] + az[26 - countEvenLast:]
			return new
		else:
			if N % 2 == 0:
				count = int(N / 2)
				new = az[:count] + az[-count:]
			if N % 2 != 0:
				if summ % 2 == 0:
					countEvenFirst = int((N + 1) / 2)
					countEvenLast = int((N - 1) / 2)
					new = az[:countEvenFirst] + az[26 - countEvenLast:]
				if summ % 2 != 0:
					countEvenFirst = int((N - 1) / 2)
					countEvenLast = int((N + 1) / 2)
					new = az[:countEvenFirst] + az[26 - countEvenLast:]
			return new


class Solution:

	def BalancedString(self, N):
		chars = 'abcdefghijklmnopqrstuvwxyz'
		is_even = 1 if sum((int(c) for c in str(N))) % 2 == 0 else -1
		res = chars * (N // 26)
		N %= 26
		if N % 2 == 0:
			res += chars[:N // 2] + chars[::-1][:N // 2][::-1]
		else:
			res += chars[:(N + is_even) // 2] + chars[::-1][:(N - is_even) // 2][::-1]
		return res


class Solution:

	def BalancedString(self, N):
		alp = 'abcdefghijklmnopqrstuvwxyz'

		def sumofdigits(N):
			count = 0
			for x in str(N):
				count += int(x)
			if count % 2 == 0:
				return 0
			else:
				return 1

		def iseven(N):
			return alp * (N // 26) + alp[:N % 26 // 2] + alp[-(N % 26 // 2):]

		def isodd(N):
			if sumofdigits(N) == 0 and N % 26 == 1:
				return alp * (N // 26) + alp[:N % 26 // 2 + 1]
			elif sumofdigits(N) == 0:
				return alp * (N // 26) + alp[:N % 26 // 2 + 1] + alp[-(N % 26 // 2):]
			else:
				return alp * (N // 26) + alp[:N % 26 // 2] + alp[-(N % 26 // 2) - 1:]
		if N == 0:
			return ''
		elif N % 26 == 0:
			return alp * (N // 26)
		elif N % 2 == 0:
			return iseven(N)
		else:
			return isodd(N)


class Solution:

	def BalancedString(self, N):
		s = 'abcdefghijklmnopqrstuvwxyz'
		ans = ''
		if N % 2 == 0:
			if N <= 26 or N < 0:
				z = N // 2
				ans += s[:z] + s[-z:]
			else:
				Q = N // 26
				R = N % 26
				if R == 0:
					ans += s * Q
				else:
					z = R // 2
					ans += s * Q + s[:z] + s[-z:]
		else:
			su = 0
			f = N
			while f != 0:
				last_digit = f % 10
				su = su + last_digit
				f = f // 10
			if su % 2 == 0:
				if N <= 26 or N < 0:
					begin = (N + 1) // 2
					end = (N - 1) // 2
					ans += s[:begin] + s[-end:]
				else:
					Q = N // 26
					R = N % 26
					if R == 1:
						begin = (R + 1) // 2
						ans += s * Q + s[:begin]
					else:
						begin = (R + 1) // 2
						end = (R - 1) // 2
						ans += s * Q + s[:begin] + s[-end:]
			elif N <= 26 or N < 0:
				begin = (N - 1) // 2
				end = (N + 1) // 2
				ans += s[:begin] + s[-end:]
			else:
				Q = N // 26
				R = N % 26
				begin = (R - 1) // 2
				end = (R + 1) // 2
				ans += s * Q + s[:begin] + s[-end:]
		return ans


class Solution:

	def BalancedString(self, N):
		ans = ''
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		while len(ans) != N:
			if N % 2 == 0:
				st = N % 26
				m = N // 26
				ans += alpha * m
				nl = st // 2
				for i in range(nl):
					ans += alpha[i]
					if len(ans) == N:
						break
				for j in range(26 - nl, 26):
					ans += alpha[j]
					if len(ans) == N:
						break
			else:
				p = N
				p = str(p)
				p = list(p)
				sm = 0
				for i in range(len(p)):
					sm += int(p[i])
				st = N % 26
				m = N // 26
				ans += alpha * m
				t1 = (st + 1) // 2
				t2 = (st - 1) // 2
				if sm % 2 == 0:
					for i in range(t1):
						ans += alpha[i]
						if len(ans) == N:
							break
					for j in range(26 - t2, 26):
						ans += alpha[j]
						if len(ans) == N:
							break
				else:
					for i in range(t2):
						ans += alpha[i]
						if len(ans) == N:
							break
					for j in range(26 - t1, 26):
						ans += alpha[j]
						if len(ans) == N:
							break
		return ans


class Solution:

	def BalancedString(self, N, N_sum=0):
		j = 'a'
		alpha_lst = []
		for i in range(26):
			alpha_lst.append(j)
			j = chr(ord(j) + 1)
		res_str = ''
		if N_sum == 0:
			for i in str(N):
				N_sum += int(i)
		if N < 26:
			if N % 2 == 0:
				for i in range(int(N / 2)):
					res_str += alpha_lst[i]
				for i in range(int(N / 2) - 1, -1, -1):
					res_str += alpha_lst[25 - i]
				return res_str
			elif N_sum % 2 == 0:
				for i in range(int((N + 1) / 2)):
					res_str += alpha_lst[i]
				for i in range(int((N - 1) / 2) - 1, -1, -1):
					res_str += alpha_lst[25 - i]
				return res_str
			else:
				for i in range(int((N - 1) / 2)):
					res_str += alpha_lst[i]
				for i in range(int((N + 1) / 2) - 1, -1, -1):
					res_str += alpha_lst[25 - i]
				return res_str
		else:
			end_str = self.BalancedString(N % 26, N_sum)
			alp_iter = 0
			for i in range(N - N % 26):
				res_str += alpha_lst[alp_iter]
				alp_iter += 1
				if alp_iter == 26:
					alp_iter = 0
			res_str += end_str
			return res_str


class Solution:

	def BalancedString(self, N):
		s = sum(list(map(int, str(N))))
		alpha = 'abcdefghijklmnopqrstuvwxyz'
		res = ''
		while N > 26:
			res = res + alpha
			N = N - 26
		if N <= 26:
			if N % 2 == 0:
				res = res + alpha[0:N // 2] + alpha[26 - N // 2:]
			elif s % 2 == 0:
				res = res + alpha[0:(N + 1) // 2] + alpha[26 - (N - 1) // 2:]
			else:
				res = res + alpha[0:(N - 1) // 2] + alpha[26 - (N + 1) // 2:]
		return res


class Solution:

	def BalancedString(self, N):
		a = 'abcdefghijklmnopqrstuvwxyz'
		s = ''
		n = N
		while N > 26:
			s = s + a
			N -= 26
		if N % 2 == 0:
			s = s + a[:N // 2]
			s = s + a[26 - N // 2:]
			return s
		else:
			p = (N - 1) // 2
			q = (N + 1) // 2
			m = str(n)
			summ = 0
			for i in m:
				summ += int(i)
			if summ % 2 == 0:
				s = s + a[:q]
				p = 26 - p
				s = s + a[p:]
				return s
			else:
				s = s + a[:p]
				q = 26 - q
				s = s + a[q:]
				return s


class Solution:

	def sum_of_dig(self, N):
		sum = 0
		for c in str(N):
			sum += int(c)
		return sum

	def BalancedString(self, N):
		ans = ''
		for i in range(N // 26):
			ans += 'abcdefghijklmnopqrstuvwxyz'
		if N % 26 == 0:
			return ans
		rem = N % 26
		if N % 2 == 0:
			l = r = rem // 2
		elif self.sum_of_dig(N) % 2 == 0:
			l = (rem + 1) // 2
			r = (rem - 1) // 2
		else:
			r = (rem + 1) // 2
			l = (rem - 1) // 2
		sym = ord('a')
		for i in range(l):
			ans += chr(sym)
			sym += 1
		sym = ord('z') + 1 - r
		for i in range(r):
			ans += chr(sym)
			sym += 1
		return ans


class Solution:

	def BalancedString(self, N):
		lcalpha = 'abcdefghijklmnopqrstuvwxyz'
		parity = sum(map(int, str(N))) % 2
		tail = (N - 1) % 26 + 1
		th = (tail + 1 - parity) // 2
		return (N - 1) // 26 * lcalpha + lcalpha[:th] + lcalpha[26 + th - tail:]