Generate a string with maximum possible alphabets with odd frequencies - GeeksforGeeks

19 Apr, 2021
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’.
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++
Java
Python3
C#
Javascript
// C++ program to generate a string// of length n with maximum possible// alphabets with each of them// occuring odd number of times. #include <bits/stdc++.h>using namespace std; // Function to generate a string// of length n with maximum possible// alphabets each occuring 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 codeint main(){    int n = 34;    cout << generateTheString(n);    return 0;}
// Java program to generate a string// of length n with maximum possible// alphabets with each of them// occuring odd number of times.import java.util.*; class GFG{     // Function to generate a string// of length n with maximum possible// alphabets each occuring 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 program to generate a string# of length n with maximum possible# alphabets with each of them# occuring odd number of times. # Function to generate a string# of length n with maximum possible# alphabets each occuring 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# program to generate a string// of length n with maximum possible// alphabets with each of them// occuring odd number of times.using System;class GFG{ // Function to generate a string// of length n with maximum possible// alphabets each occuring 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 codepublic static void Main(){    int n = 34;    Console.Write(generateTheString(n));}} // This code is contributed by Nidhi_Biet
<script> // Javascript program to generate a string// of length n with maximum possible// alphabets with each of them// occuring odd number of times. // Function to generate a string// of length n with maximum possible// alphabets each occuring 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 codevar n = 34;document.write( generateTheString(n)); </script>
bcdefghijklmnopqrstuvwxyzaaaaaaaaa
 
ukasp
nidhi_biet
offbeat
noob2000
Competitive Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Shortest path in a directed graph by Dijkstra’s algorithm
Breadth First Traversal ( BFS ) on a 2D array
Multistage Graph (Shortest Path)
Runtime Errors
Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types