Given an integer n
, return a string with n
characters such that each character in such string occurs an odd number of times.
The returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them.
Example 1:
Input: n = 4 Output: "pppz" Explanation: "pppz" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as "ohhh" and "love".
Example 2:
Input: n = 2 Output: "xy" Explanation: "xy" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as "ag" and "ur".
Example 3:
Input: n = 7 Output: "holasss"
Constraints:
1 <= n <= 500
The problem asks to generate a string of length n
where each character appears an odd number of times. The solution leverages a simple observation:
Odd Length (n): If n
is odd, you can simply create a string of all the same character (e.g., all 'a'). Each character will appear n
times, and n
is odd, satisfying the condition.
Even Length (n): If n
is even, you can create a string with n-1
occurrences of one character (e.g., 'a') and one occurrence of a different character (e.g., 'b'). 'a' will appear an odd number of times (n-1
), and 'b' will appear once (also odd).
The code implements this directly. It checks if n
is odd or even and constructs the appropriate string accordingly.
The code examples below demonstrate the solution in several programming languages. The core logic remains consistent across all languages:
Python:
class Solution:
def generateTheString(self, n: int) -> str:
return 'a' * n if n & 1 else 'a' * (n - 1) + 'b'
n & 1
: This is a bitwise AND operation. It's a concise way to check if n
is odd (odd numbers have their least significant bit set to 1).'a' * n
: Creates a string of n
'a' characters if n
is odd.'a' * (n - 1) + 'b'
: Creates a string of n-1
'a's followed by one 'b' if n
is even.Java:
class Solution {
public String generateTheString(int n) {
return (n % 2 == 1) ? "a".repeat(n) : "a".repeat(n - 1) + "b";
}
}
n % 2 == 1
: Checks if n
is odd using the modulo operator."a".repeat(n)
: Java's repeat()
method efficiently creates a repeated string.C++:
class Solution {
public:
string generateTheString(int n) {
string ans(n, 'a'); // Initialize a string of n 'a's
if (n % 2 == 0) { //If n is even
ans[0] = 'b'; //Change the first character to 'b'
}
return ans;
}
};
string ans(n, 'a');
: Creates a string of size n
filled with 'a' characters. This is efficient in C++.Go:
func generateTheString(n int) string {
ans := strings.Repeat("a", n-1)
if n%2 == 0 {
ans += "b"
} else {
ans += "a"
}
return ans
}
strings.Repeat("a", n-1)
: Go's strings.Repeat
function is used for string repetition.TypeScript:
function generateTheString(n: number): string {
const ans = Array(n).fill('a');
if (n % 2 === 0) {
ans[0] = 'b';
}
return ans.join('');
}
Array(n).fill('a')
: Creates an array of size n
filled with 'a' and then joins it into a string.Time Complexity: O(n) - The dominant operation is string construction, which takes linear time in the length of the string.
Space Complexity: O(n) - The space used is proportional to the length of the generated string.
In summary, the solution is efficient and straightforward, directly addressing the problem constraints with minimal overhead.