Skip to main content
Back to problems
#1374
Easy Algorithms

Generate a string with characters that have odd counts

String
78.5% acceptance
Feb 25, 2026
527
1287
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.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn generate_the_string(n: i32) -> String {
    if n % 2 == 1 {
      "a".repeat(n as usize)
    } else {
      "a".repeat(n as usize - 1) + "b"
    }
  }
}