Home Generate a String With Characters That Have Odd Counts
Post
Cancel

Generate a String With Characters That Have Odd Counts

View Generate a String With Characters That Have Odd Counts on LeetCode

Statistics

Time Spent Coding
3 minutes

Time Complexity
O(n) - The construction of the string takes O(n) time, resulting in the O(n) time complexity.

Space Complexity
O(n) - If the result string is counted in the space complexity, then the space complexity is O(n), otherwise it is O(1).

Runtime Beats
98.43% of other submissions

Explanation

The comments explain the program

Solution

1
2
3
4
5
6
7
8
9
class Solution:
    def generateTheString(self, n: int) -> str:
        # If n is odd, return `a` n times
        if n%2 != 0:
            return 'a' * n

        # Otherwise return `a` n-1 times and `b`
        else:
            return 'a' * (n - 1) + 'b'
This post is licensed under CC BY 4.0 by the author.

Relative Sort Array

Squares of a Sorted Array