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:



Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 code 
int main() 
    int n = 34; 
    cout << generateTheString(n); 
    return 0; 
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 code
public static void main(String[] args)
{
    int n = 34;
      
    System.out.println(generateTheString(n));
}
}
  
// This code is contributed by offbeat
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 code
if __name__ == "__main__":
      
    n = 34
    print(generateTheString(n))
  
# This code is contributed by chitranayal
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 code 
public static void Main() 
    int n = 34; 
    Console.Write(generateTheString(n)); 
  
// This code is contributed by Nidhi_Biet 
chevron_right

Output:
bcdefghijklmnopqrstuvwxyzaaaaaaaaa




Article Tags :
Practice Tags :