array and hashmap November 29, 2022

Generate a string with characters that have odd counts

Time O(n) Space O(1) Open original problem

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)