Lexicographically first alternate vowel and consonant string | Java implementation of lexicographically first alternate vowel and consonant String ; ' ch ' is vowel or not ; create alternate vowel and consonant String str1 [ 0. . . l1 - 1 ] and str2 [ start ... l2 - 1 ] ; first adding character of vowel / consonant then adding character of consonant / vowel ; function to find the required lexicographically first alternate vowel and consonant String ; hash table to store frequencies of each character in ' str ' ; initialize all elements of char_freq [ ] to 0 ; count vowels ; count consonants ; update frequency of ' ch ' in char_freq [ ] ; no such String can be formed ; form the vowel String ' vstr ' and consonant String ' cstr ' which contains characters in lexicographical order ; remove first character of vowel String then create alternate String with cstr [ 0. . . nc - 1 ] and vstr [ 1. . . nv - 1 ] ; remove first character of consonant String then create alternate String with vstr [ 0. . . nv - 1 ] and cstr [ 1. . . nc - 1 ] ; if both vowel and consonant strings are of equal length start creating String with consonant ; start creating String with vowel ; Driver code
import java . util . Arrays ; class GFG { static final int SIZE = 26 ; static boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) { return true ; } return false ; } static String createAltStr ( String str1 , String str2 , int start , int l ) { String finalStr = " " ; for ( int i = 0 , j = start ; j < l ; i ++ , j ++ ) { finalStr = ( finalStr + str1 . charAt ( i ) ) + str2 . charAt ( j ) ; } return finalStr ; } static String findAltStr ( String str ) { int char_freq [ ] = new int [ SIZE ] ; Arrays . fill ( char_freq , 0 ) ; int nv = 0 , nc = 0 ; String vstr = " " , cstr = " " ; int l = str . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { char ch = str . charAt ( i ) ; if ( isVowel ( ch ) ) { nv ++ ; } else { nc ++ ; } char_freq [ ch - 97 ] ++ ; } if ( Math . abs ( nv - nc ) >= 2 ) { return " no ▁ such ▁ String " ; } for ( int i = 0 ; i < SIZE ; i ++ ) { char ch = ( char ) ( i + 97 ) ; for ( int j = 1 ; j <= char_freq [ i ] ; j ++ ) { if ( isVowel ( ch ) ) { vstr += ch ; } else { cstr += ch ; } } } if ( nv > nc ) { return ( vstr . charAt ( 0 ) + createAltStr ( cstr , vstr , 1 , nv ) ) ; } if ( nc > nv ) { return ( cstr . charAt ( 0 ) + createAltStr ( vstr , cstr , 1 , nc ) ) ; } if ( cstr . charAt ( 0 ) < vstr . charAt ( 0 ) ) { return createAltStr ( cstr , vstr , 0 , nv ) ; } return createAltStr ( vstr , cstr , 0 , nc ) ; } public static void main ( String [ ] args ) { String str = " aeroplane " ; System . out . println ( findAltStr ( str ) ) ; } }