Count number of rotated strings which have more number of vowels in the first half than second half | Java implementation of the approach ; Function to return the count of rotated Strings which have more number of vowels in the first half than the second half ; Create a new String ; Pre array to store count of all vowels ; Compute the prefix array ; To store the required answer ; Find all rotated Strings ; Right and left index of the String ; x1 stores the number of vowels in the rotated String ; Left stores the number of vowels in the first half of rotated String ; Right stores the number of vowels in the second half of rotated String ; If the count of vowels in the first half is greater than the count in the second half ; Return the required answer ; Driver code
class GFG { static int cntRotations ( String s , int n ) { String str = s + s ; int pre [ ] = new int [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( i != 0 ) pre [ i ] += pre [ i - 1 ] ; if ( str . charAt ( i ) == ' a ' || str . charAt ( i ) == ' e ' || str . charAt ( i ) == ' i ' || str . charAt ( i ) == ' o ' || str . charAt ( i ) == ' u ' ) { pre [ i ] ++ ; } } int ans = 0 ; for ( int i = n - 1 ; i < 2 * n - 1 ; i ++ ) { int r = i , l = i - n ; int x1 = pre [ r ] ; if ( l >= 0 ) x1 -= pre [ l ] ; r = i - n / 2 ; int left = pre [ r ] ; if ( l >= 0 ) left -= pre [ l ] ; int right = x1 - left ; if ( left > right ) { ans ++ ; } } return ans ; } public static void main ( String args [ ] ) { String s = " abecidft " ; int n = s . length ( ) ; System . out . println ( cntRotations ( s , n ) ) ; } }