Encrypt string with product of number of vowels and consonants in substring of size k | isVowel ( ) is a function that returns true for a vowel and false otherwise . ; function to Encrypt the String ; cv to count vowel cc to count consonants ; Counting prefix count of vowel and prefix count of consonants ; generating the encrypted String . ; Driver Code
function isVowel ( c ) { return c === " " || c === " " || c === " " || c === " " || c === " " ; } function encryptString ( s , n , k ) { var cv = new Array ( n ) . fill ( 0 ) ; var cc = new Array ( n ) . fill ( 0 ) ; if ( isVowel ( s [ 0 ] ) ) cv [ 0 ] = 1 ; else cc [ 0 ] = 1 ; for ( var i = 1 ; i < n ; i ++ ) { cv [ i ] = cv [ i - 1 ] + ( isVowel ( s [ i ] ) === true ? 1 : 0 ) ; cc [ i ] = cc [ i - 1 ] + ( isVowel ( s [ i ] ) === true ? 0 : 1 ) ; } var ans = " " ; var prod = 0 ; prod = cc [ k - 1 ] * cv [ k - 1 ] ; ans += prod ; for ( var i = k ; i < s . length ; i ++ ) { prod = ( cc [ i ] - cc [ i - k ] ) * ( cv [ i ] - cv [ i - k ] ) ; ans += prod ; } return ans ; } var s = " " ; var n = s . length ; var k = 2 ; document . write ( encryptString ( s . split ( " " ) , n , k ) + " " ) ;