Generate a String With Characters That Have Odd Counts

Problem Id: 1374 Difficulty: Easy Tag: String


Intuition

If n id odd, then we could directly return 'a' * n. And if n is even, we could return 'a' * (n - 1) + 'b', because n - 1 is odd.

Solution


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