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++
#include <bits/stdc++.h>
using namespace std;
string generateTheString( int n)
{
string ans= "" ;
if (n%2)
{
for ( int i=0;i<min(n,24);i++)
{
ans+=( char )( 'b' + i);
}
if (n>24)
{
for ( int i=0;i<(n-24);i++)
ans+= 'a' ;
}
}
else
{
for ( int i=0;i<min(n,25);i++)
{
ans+=( char )( 'b' + i);
}
if (n>25)
{
for ( int i=0;i<(n-25);i++)
ans+= 'a' ;
}
}
return ans;
}
int main()
{
int n = 34;
cout << generateTheString(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String generateTheString( int n)
{
String ans = "" ;
if (n % 2 != 0 )
{
for ( int i = 0 ;
i < Math.min(n, 24 ); i++)
{
ans += ( char )( 'b' + i);
}
if (n > 24 )
{
for ( int i = 0 ;
i < (n - 24 ); i++)
ans += 'a' ;
}
}
else
{
for ( int i = 0 ;
i < Math.min(n, 25 ); i++)
{
ans += ( char )( 'b' + i);
}
if (n > 25 )
{
for ( int i = 0 ;
i < (n - 25 ); i++)
ans += 'a' ;
}
}
return ans;
}
public static void main(String[] args)
{
int n = 34 ;
System.out.println(generateTheString(n));
}
}
|
Python3
def generateTheString( n):
ans = ""
if (n % 2 ):
for i in range ( min (n, 24 )):
ans + = chr ( ord ( 'b' ) + i)
if (n > 24 ):
for i in range ((n - 24 )):
ans + = 'a'
else :
for i in range ( min (n, 25 )):
ans + = chr ( ord ( 'b' ) + i)
if (n > 25 ):
for i in range ((n - 25 )):
ans + = 'a'
return ans
if __name__ = = "__main__" :
n = 34
print (generateTheString(n))
|
C#
using System;
class GFG{
static string generateTheString( int n)
{
string ans = "" ;
if (n % 2 == 0)
{
for ( int i = 0; i < Math.Min(n, 24); i++)
{
ans += ( char )( 'b' + i);
}
if (n > 24)
{
for ( int i = 0; i < (n - 24); i++)
ans += 'a' ;
}
}
else
{
for ( int i = 0; i < Math.Min(n, 25); i++)
{
ans += ( char )( 'b' + i);
}
if (n > 25)
{
for ( int i = 0; i < (n - 25); i++)
ans += 'a' ;
}
}
return ans;
}
public static void Main()
{
int n = 34;
Console.Write(generateTheString(n));
}
}
|
Javascript
<script>
function generateTheString(n)
{
var ans= "" ;
if (n%2)
{
for ( var i=0;i<min(n,24);i++)
{
ans+=(char)( 'b' + i);
}
if (n>24)
{
for ( var i=0;i<(n-24);i++)
ans+= 'a' ;
}
}
else
{
for ( var i=0;i<Math.min(n,25);i++)
{
ans+= String.fromCharCode( 'b' .charCodeAt(0) + i);
}
if (n>25)
{
for ( var i=0;i<(n-25);i++)
ans+= 'a' ;
}
}
return ans;
}
var n = 34;
document.write( generateTheString(n));
</script>
|
Output:
bcdefghijklmnopqrstuvwxyzaaaaaaaaa
Time Complexity: O(n)
Auxiliary Space: O(n), where n is a given integer.
Share your thoughts in the comments
Please Login to comment...