Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1374. Generate a String With Characters That Have Odd Counts - Leetcode Solution

Problem Description

The problem "Generate a String With Characters That Have Odd Counts" asks you to construct a string of length n using only lowercase English letters such that each character in the string appears an odd number of times. You can use any lowercase English letters, and you only need to return any valid string that satisfies the condition.

  • The input is a single integer n (1 ≤ n ≤ 1000).
  • The output is a string of length n where the count of every character used is odd.
  • There is always at least one valid solution.

Thought Process

To solve this problem, we need to ensure that for every character used in our result string, the number of times it appears is an odd number.

At first glance, one might think of trying all possible combinations or distributing the characters in some balanced way. However, this would be unnecessarily complicated. Instead, we should look for a pattern or shortcut that guarantees the odd-count property.

The key realization is that any single letter repeated an odd number of times will naturally have an odd count. If n is odd, we can simply repeat any letter n times. If n is even, we can use one letter n-1 times (which is odd) and a different letter once (also odd), thus ensuring all counts remain odd.

Solution Approach

  • Step 1: Check if n is odd or even.
  • Step 2:
    • If n is odd, pick any letter (e.g., 'a') and repeat it n times. The count for 'a' will be n (odd).
    • If n is even, pick one letter (e.g., 'a') and use it n-1 times (which is odd), and pick another letter (e.g., 'b') and use it once (also odd). This way, both 'a' and 'b' have odd counts.
  • Step 3: Return the constructed string.

This approach is efficient because it only requires a simple check and string construction, with no need for complex data structures or brute-force attempts.

Example Walkthrough

Example 1: n = 4

  • Since 4 is even, use 'a' three times (odd) and 'b' once (odd):
  • Result: "aaab" (Counts: 'a' = 3, 'b' = 1; both are odd)

Example 2: n = 7

  • Since 7 is odd, use 'a' seven times:
  • Result: "aaaaaaa" (Count: 'a' = 7, which is odd)

In both cases, the solution is found by simply checking the parity of n and constructing the string accordingly.

Code Implementation

class Solution:
    def generateTheString(self, n: int) -> str:
        if n % 2 == 1:
            return 'a' * n
        else:
            return 'a' * (n - 1) + 'b'
      
class Solution {
public:
    string generateTheString(int n) {
        if (n % 2 == 1) {
            return string(n, 'a');
        } else {
            return string(n - 1, 'a') + "b";
        }
    }
};
      
class Solution {
    public String generateTheString(int n) {
        if (n % 2 == 1) {
            return "a".repeat(n);
        } else {
            return "a".repeat(n - 1) + "b";
        }
    }
}
      
var generateTheString = function(n) {
    if (n % 2 === 1) {
        return 'a'.repeat(n);
    } else {
        return 'a'.repeat(n - 1) + 'b';
    }
};
      

Time and Space Complexity

  • Brute-force approach: If you tried to generate all possible strings and check their counts, the time complexity would be exponential (O(26^n)), which is infeasible.
  • Optimized approach (our solution):
    • Time Complexity: O(n), because we construct a string of length n by repeating characters.
    • Space Complexity: O(n), for storing the output string.
  • Because we use only basic string operations and no extra data structures, this is as efficient as possible for this problem.

Summary

The solution to "Generate a String With Characters That Have Odd Counts" leverages the insight that using a single letter (for odd n) or two letters (for even n) guarantees all character counts are odd. This approach is both simple and optimal, requiring only a parity check and basic string operations. It demonstrates how recognizing problem constraints can lead to elegant and highly efficient solutions.