Generate a String With Characters That Have Odd Counts - Problem

Given an integer n, return a string with n characters such that each character in the string occurs an odd number of times.

The returned string must contain only lowercase English letters. If there are multiple valid strings, return any of them.

Input & Output

Example 1 — Odd Length
$ Input: n = 3
Output: aaa
💡 Note: String has 3 characters, character 'a' appears 3 times (odd count). Any single character repeated 3 times works.
Example 2 — Even Length
$ Input: n = 4
Output: aaab
💡 Note: String has 4 characters, 'a' appears 3 times (odd), 'b' appears 1 time (odd). Both characters have odd counts.
Example 3 — Minimum Case
$ Input: n = 1
Output: a
💡 Note: Single character string, 'a' appears 1 time (odd count). This is the smallest valid case.

Constraints

  • 1 ≤ n ≤ 500

Visualization

Tap to expand
Generate String with Odd Character Countsn = 4(even)Apply Pattern:(n-1)a + 1baaabresultInputAlgorithmOutputVerification: a appears 3 times (odd), b appears 1 time (odd)✓ All characters have odd counts
Understanding the Visualization
1
Input
Integer n specifying string length
2
Process
Apply pattern based on n's parity
3
Output
String with all characters having odd counts
Key Takeaway
🎯 Key Insight: Use n's parity to determine pattern - odd n uses one character, even n uses two characters with specific counts
Asked in
Google 15 Amazon 12 Microsoft 8
25.8K Views
Medium Frequency
~10 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen