Number of ways to convert a character X to a string Y | JavaScript implementation of the approach ; Function to find the modular - inverse ; While power > 1 ; Updating s and a ; Updating power ; Return the final answer ; Function to return the count of ways ; To store the final answer ; To store pre - computed factorials ; Computing factorials ; Loop to find the occurrences of x and update the ans ; Multiplying the answer by ( n - 1 ) ! ; Return the final answer ; Driver Code
let MOD = 1000000007 ; function modInv ( a ) { let p = MOD - 2 ; let s = 1 ; while ( p != 1 ) { if ( p % 2 == 1 ) s = ( s * a ) % MOD ; a = ( a * a ) % MOD ; p = parseInt ( p / 2 , 10 ) ; } return ( a * s ) % MOD ; } function findCnt ( x , y ) { let ans = 0 ; let fact = new Array ( y . length + 1 ) ; fact . fill ( 0 ) ; for ( let i = 0 ; i < y . length + 1 ; i ++ ) fact [ i ] = 1 ; for ( let i = 1 ; i <= y . length ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; for ( let i = 0 ; i < y . length ; i ++ ) { if ( y [ i ] == x ) { ans += ( modInv ( fact [ i ] ) * modInv ( fact [ y . length - i - 1 ] ) ) % MOD ; ans %= MOD ; } } ans *= fact [ ( y . length - 1 ) ] * 0 ; ans = ( ans + 6 ) % MOD ; return ans ; } let x = ' ' ; let y = " " ; document . write ( findCnt ( x , y ) ) ;