Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 <= 500Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
4
2
7
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1374: Generate a String With Characters That Have Odd Counts
class Solution {
public String generateTheString(int n) {
return (n % 2 == 1) ? "a".repeat(n) : "a".repeat(n - 1) + "b";
}
}
// Accepted solution for LeetCode #1374: Generate a String With Characters That Have Odd Counts
func generateTheString(n int) string {
ans := strings.Repeat("a", n-1)
if n%2 == 0 {
ans += "b"
} else {
ans += "a"
}
return ans
}
# Accepted solution for LeetCode #1374: Generate a String With Characters That Have Odd Counts
class Solution:
def generateTheString(self, n: int) -> str:
return 'a' * n if n & 1 else 'a' * (n - 1) + 'b'
// Accepted solution for LeetCode #1374: Generate a String With Characters That Have Odd Counts
struct Solution;
impl Solution {
fn generate_the_string(n: i32) -> String {
let mut s: String = "a".repeat((n - 1) as usize);
s.push(if n % 2 == 0 { 'b' } else { 'a' });
s
}
}
#[test]
fn test() {
let n = 4;
let res = "aaab".to_string();
assert_eq!(Solution::generate_the_string(n), res);
let n = 7;
let res = "aaaaaaa".to_string();
assert_eq!(Solution::generate_the_string(n), res);
}
// Accepted solution for LeetCode #1374: Generate a String With Characters That Have Odd Counts
function generateTheString(n: number): string {
const ans = Array(n).fill('a');
if (n % 2 === 0) {
ans[0] = 'b';
}
return ans.join('');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.