Generate a string with maximum possible alphabets with odd frequencies

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 <bits/stdc++.h> 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;i<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<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 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 codepublic 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 codeif __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
<script>Â
// Javascript 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. function generateTheString(n) {     var ans="";     // If n is odd     if(n%2)     {         // Add all characters from         // b-y         for(var i=0;i<min(n,24);i++)         {             ans+=(char)('b' + i);         }         // Append a to fill the         // remaining length         if(n>24)         {             for(var i=0;i<(n-24);i++)                 ans+='a';         }     }     // If n is even     else    {         // Add all characters from         // b-z         for(var i=0;i<Math.min(n,25);i++)         {             ans+= String.fromCharCode('b'.charCodeAt(0) + i);         }         // Append a to fill the         // remaining length         if(n>25)         {             for(var i=0;i<(n-25);i++)                 ans+='a';         }     }          return ans; } Â
// Driven code var n = 34; document.write( generateTheString(n)); Â
</script> |
bcdefghijklmnopqrstuvwxyzaaaaaaaaa
Â
Time Complexity: O(n)
Auxiliary Space: O(n), where n is a given integer.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



