Generate a string with maximum possible alphabets with odd frequencies | C ++ 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 . ; If n is odd ; Add all characters from b - y ; Append a to fill the remaining length ; If n is even ; Add all characters from b - z ; Append a to fill the remaining length ; Driven code
#include <bits/stdc++.h> NEW_LINE 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 ; }