Count number of rotated strings which have more number of vowels in the first half than second half | 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
function cntRotations ( s , n ) { let str = s + s ; let pre = new Array ( 2 * n ) ; pre . fill ( 0 ) ; for ( let i = 0 ; i < 2 * n ; i ++ ) { if ( i != 0 ) pre [ i ] += pre [ i - 1 ] ; if ( str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' str [ i ] == ' ' ) { pre [ i ] ++ ; } } let ans = 0 ; for ( let i = n - 1 ; i < 2 * n - 1 ; i ++ ) { let r = i , l = i - n ; let x1 = pre [ r ] ; if ( l >= 0 ) x1 -= pre [ l ] ; r = i - parseInt ( n / 2 , 10 ) ; let left = pre [ r ] ; if ( l >= 0 ) left -= pre [ l ] ; let right = x1 - left ; if ( left > right ) { ans ++ ; } } return ans ; } let s = " " ; let n = s . length ; document . write ( cntRotations ( s , n ) ) ;