[LeetCode]1374. Generate a String With Characters That Have Odd Counts

Input: n = 4
Output: "pppz"
  1. If n is even, add “a” to n-1 location and add “b” in last location.
  2. if n is odd , add “a” in all location.
class Solution:
def generateTheString(self, n):
ans=[]
if n%2==0:
for i in range(n-1):
ans.append('a')
ans.append('b')
else:
for i in range(n):
ans.append('a')
return ans
class Solution:
def generateTheString(self, n):
ans=[]
if n%2==0:
ans=['a' for i in range(n-1)]
ans.append('b')
else:
ans=['a' for i in range(n)]
return ans

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store