Smallest string of length n where every letter appears an odd number of times

Quick Overview

Given a length n up to 500, return the lexicographically smallest lowercase string in which every letter that appears occurs an odd number of times. It tests parity reasoning and a short argument that the constructed string is the minimal valid one.

Smallest string of length n where every letter appears an odd number of times

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer `n`, build a string of exactly `n` characters in which every character that appears occurs an odd number of times. ### Function Signature ```python def odd_count_string(n: int) -> str: ``` ### Rules - The string may contain only lowercase English letters `a` to `z`. - Usually several strings qualify. Return the lexicographically smallest one, so the expected output is unique. ### Constraints - `1 <= n <= 500` ### Examples ```text Input: n = 4 Output: "aaab" ``` `'a'` occurs 3 times and `'b'` occurs once, and both counts are odd. The only smaller string of length 4 is `"aaaa"`, which fails because `'a'` would occur 4 times. ```text Input: n = 2 Output: "ab" ``` Each letter occurs once. The only smaller candidate is `"aa"`, where the count of `'a'` is even. ```text Input: n = 7 Output: "aaaaaaa" ``` Seven is odd, so one letter repeated 7 times already qualifies, and no string of length 7 is smaller.

Overview: Given a length n up to 500, return the lexicographically smallest lowercase string in which every letter that appears occurs an odd number of times. It tests parity reasoning and a short argument that the constructed string is the minimal valid one.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer n, build a string of exactly n characters in which every character that appears occurs an odd number of times.

Function Signature

def odd_count_string(n: int) -> str:

Rules

  • The string may contain only lowercase English letters a to z .
  • Usually several strings qualify. Return the lexicographically smallest one, so the expected output is unique.

Constraints

  • 1 <= n <= 500

Examples

Input: n = 4
Output: "aaab"

'a' occurs 3 times and 'b' occurs once, and both counts are odd. The only smaller string of length 4 is "aaaa", which fails because 'a' would occur 4 times.

Input: n = 2
Output: "ab"

Each letter occurs once. The only smaller candidate is "aa", where the count of 'a' is even.

Input: n = 7
Output: "aaaaaaa"

Seven is odd, so one letter repeated 7 times already qualifies, and no string of length 7 is smaller.