Find the player to last modify a string such that even number of consonants and no vowels are left in the string | Java program for the above approach ; Function to find a winner of the game if both the player plays optimally ; Stores the count of vowels and consonants ; Traverse the String ; Check if character is vowel ; Increment vowels count ; Otherwise increment the consonants count ; Check if Player B wins ; Check if Player A wins ; Check if Player A wins ; If game ends in a Draw ; Driver Code ; Given String s ; Function Call
class GFG { static void findWinner ( char [ ] s ) { int vowels_count = 0 , consonants_count = 0 ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { vowels_count ++ ; } else { consonants_count ++ ; } } if ( vowels_count == 0 ) { if ( consonants_count % 2 == 0 ) { System . out . print ( " Player ▁ B " ) ; } else { System . out . print ( " Player ▁ A " ) ; } } else if ( vowels_count == 1 && consonants_count % 2 != 0 ) { System . out . print ( " Player ▁ A " ) ; } else { System . out . print ( " D " ) ; } } public static void main ( String [ ] args ) { String s = " abcd " ; findWinner ( s . toCharArray ( ) ) ; } }