Generate a String With Characters That Have Odd Counts ( LeetCode ) Problem Solution.

Generate a String With Characters That Have Odd Counts ( LeetCode ) Problem Solution.

Example 1:
Input: n = 4
Output: "pppz"
Explanation: "pppz" may be a valid string since the character 'p' occurs 3 times and therefore the character 'z' occurs once. Note that there are many other valid strings like "ohhh" and "love".
Example 2:
Input: n = 2
Output: "xy"
Explanation: "xy" may be a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings like "ag" and "ur".
Example 3:
Input: n = 7
Output: "holasss"

Constraints:
  • 1 <= n <= 500
Solution:-


class Solution:

    def generateTheString(self, n: int) -> str:
            s=''
            if(n%2==0):
                for i in range(0,n):
                    if(i!=(n-1)):
                        s+='a'
                    else:
                        s+='b'
            else:
                for i in range(0,n):
                    s+='a'

            return s

Runtime: 28 ms, faster than 66.38% of Python3 online submissions for Generate a String With Characters That Have Odd Counts.
Memory Usage: 13.8 MB, but 100.00% of Python3 online submissions for Generate a String With Characters That Have Odd Counts.