# Generate a string with maximum possible alphabets with odd frequencies

Last Updated : 30 Dec, 2022

Given an integer N, the task is to generate a string str which contains maximum possible lowercase alphabets with each of them appearing an odd number of times.
Examples:

Input: N = 17
Output: bcdefghijklmnopqr
Explanation: In order to maximize the number of characters, any 17 characters can be selected and made to appear once. Thus, abcdefghijklmnopq, bcdefghijklmnopqx, etc can be also be valid outputs.
Input: N = 35
Output: bcdefghijklmnopqrstuvwxyaaaaaaaaaaa
Explanation: In order to maximize the number of characters, add any 24 different characters once, and fill the remaining length by any other character.

Approach:

• If N is less than equal to 26, we fill the string by N different characters each appearing once.
• Otherwise:
• If N is odd, we add all 24 characters from ‘b’-‘y’ once and fill the remaining odd length by ‘a’.
• If N is even, we add all 25 characters from ‘b’-‘z’ once and fill the remaining odd length by ‘a’.

Below is the implementation of the above approach:

## C++

 `// C++ program to generate a string ` `// of length n with maximum possible ` `// alphabets with each of them ` `// occurring odd number of times. `   `#include ` `using` `namespace` `std; `   `// Function to generate a string ` `// of length n with maximum possible ` `// alphabets each occurring odd ` `// number of times. ` `string generateTheString(``int` `n) ` `{ ` `    ``string ans=``""``; ` `    ``// If n is odd ` `    ``if``(n%2) ` `    ``{ ` `        ``// Add all characters from ` `        ``// b-y ` `        ``for``(``int` `i=0;i24) ` `        ``{ ` `            ``for``(``int` `i=0;i<(n-24);i++) ` `                ``ans+=``'a'``; ` `        ``} ` `    ``} ` `    ``// If n is even ` `    ``else` `    ``{ ` `        ``// Add all characters from ` `        ``// b-z ` `        ``for``(``int` `i=0;i25) ` `        ``{ ` `            ``for``(``int` `i=0;i<(n-25);i++) ` `                ``ans+=``'a'``; ` `        ``} ` `    ``} ` `    `  `    ``return` `ans; ` `} `   `// Driven code ` `int` `main() ` `{ ` `    ``int` `n = 34; ` `    ``cout << generateTheString(n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to generate a string` `// of length n with maximum possible` `// alphabets with each of them` `// occurring odd number of times.` `import` `java.util.*;`   `class` `GFG{` `    `  `// Function to generate a string` `// of length n with maximum possible` `// alphabets each occurring odd` `// number of times.` `static` `String generateTheString(``int` `n)` `{` `    ``String ans = ``""``;` `    `  `    ``// If n is odd` `    ``if` `(n % ``2` `!= ``0``)` `    ``{`   `        ``// Add all characters from` `        ``// b-y` `        ``for``(``int` `i = ``0``; ` `                ``i < Math.min(n, ``24``); i++)` `        ``{` `            ``ans += (``char``)(``'b'` `+ i);` `        ``}` `        `  `        ``// Append a to fill the` `        ``// remaining length` `        ``if` `(n > ``24``) ` `        ``{` `            ``for``(``int` `i = ``0``; ` `                    ``i < (n - ``24``); i++)` `                ``ans += ``'a'``;` `        ``}` `    ``}` `    `  `    ``// If n is even` `    ``else` `    ``{` `        `  `        ``// Add all characters from` `        ``// b-z` `        ``for``(``int` `i = ``0``; ` `                ``i < Math.min(n, ``25``); i++)` `        ``{` `            ``ans += (``char``)(``'b'` `+ i);` `        ``}`   `        ``// Append a to fill the` `        ``// remaining length` `        ``if` `(n > ``25``) ` `        ``{` `            ``for``(``int` `i = ``0``; ` `                    ``i < (n - ``25``); i++)` `                ``ans += ``'a'``;` `        ``}` `    ``}` `    ``return` `ans;` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `n = ``34``;` `    `  `    ``System.out.println(generateTheString(n));` `}` `}`   `// This code is contributed by offbeat`

## Python3

 `# Python3 program to generate a string ` `# of length n with maximum possible` `# alphabets with each of them` `# occurring odd number of times.`   `# Function to generate a string` `# of length n with maximum possible ` `# alphabets each occurring odd ` `# number of times.` `def` `generateTheString( n):`   `    ``ans ``=` `""` `    `  `    ``# If n is odd` `    ``if``(n ``%` `2``):` `        `  `        ``# Add all characters from` `        ``# b-y` `        ``for` `i ``in` `range``(``min``(n, ``24``)):` `            ``ans ``+``=` `chr``(``ord``(``'b'``) ``+` `i)` `        `  `        ``# Append a to fill the ` `        ``# remaining length` `        ``if``(n > ``24``):` `            ``for` `i ``in` `range``((n ``-` `24``)):` `                ``ans ``+``=` `'a'` `        `  `    ``# If n is even` `    ``else``:` `        `  `        ``# Add all characters from` `        ``# b-z` `        ``for` `i ``in` `range``(``min``(n, ``25``)):` `            ``ans ``+``=` `chr``(``ord``(``'b'``) ``+` `i)` `        `  `        ``# Append a to fill the ` `        ``# remaining length` `        ``if``(n > ``25``):` `            ``for` `i ``in` `range``((n ``-` `25``)):` `                ``ans ``+``=` `'a'` `    ``return` `ans`   `# Driver code` `if` `__name__ ``=``=` `"__main__"``:` `    `  `    ``n ``=` `34` `    ``print``(generateTheString(n))`   `# This code is contributed by chitranayal`

## C#

 `// C# program to generate a string ` `// of length n with maximum possible ` `// alphabets with each of them ` `// occurring odd number of times. ` `using` `System; ` `class` `GFG{ `   `// Function to generate a string ` `// of length n with maximum possible ` `// alphabets each occurring odd ` `// number of times. ` `static` `string` `generateTheString(``int` `n) ` `{ ` `    ``string` `ans = ``""``; ` `    `  `    ``// If n is odd ` `    ``if``(n % 2 == 0) ` `    ``{ ` `        ``// Add all characters from ` `        ``// b-y ` `        ``for``(``int` `i = 0; i < Math.Min(n, 24); i++) ` `        ``{ ` `            ``ans += (``char``)(``'b'` `+ i); ` `        ``} ` `        `  `        ``// Append a to fill the ` `        ``// remaining length ` `        ``if``(n > 24) ` `        ``{ ` `            ``for``(``int` `i = 0; i < (n - 24); i++) ` `                ``ans += ``'a'``; ` `        ``} ` `    ``} ` `    `  `    ``// If n is even ` `    ``else` `    ``{ ` `        ``// Add all characters from ` `        ``// b-z ` `        ``for``(``int` `i = 0; i < Math.Min(n, 25); i++) ` `        ``{ ` `            ``ans += (``char``)(``'b'` `+ i); ` `        ``} ` `        `  `        ``// Append a to fill the ` `        ``// remaining length ` `        ``if``(n > 25) ` `        ``{ ` `            ``for``(``int` `i = 0; i < (n - 25); i++) ` `                ``ans += ``'a'``; ` `        ``} ` `    ``} ` `    ``return` `ans; ` `} `   `// Driven code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 34; ` `    ``Console.Write(generateTheString(n)); ` `} ` `} `   `// This code is contributed by Nidhi_Biet `

## Javascript

 ``

Output:

`bcdefghijklmnopqrstuvwxyzaaaaaaaaa`

Time Complexity: O(n)
Auxiliary Space: O(n), where n is a given integer.

Previous
Next