Generate a string with characters that have odd counts

November 29, 2022

array-and-hashmap

Problem URL: Generate a string with characters that have odd counts

If n is odd, we will return a string with n 'a's. If n is even, we will return a string with n-1 'a's and one 'b'.

class Solution:
    def generateTheString(self, n: int) -> str:
        return 'a'*(n-1) + 'b' if n % 2 == 0 else 'a'*n

Time complexity: O(n), n is the number of characters in the string
Space complexity: O(1)