Sunday, October 26, 2025
HomeData Modelling & AIGenerate a string with maximum possible alphabets with odd frequencies

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 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




<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>


Output: 

bcdefghijklmnopqrstuvwxyzaaaaaaaaa

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS