Count of non | Iterative Function to calculate base ^ pow in O ( log y ) ; Function to return the count of non palindromic strings ; Count of strings using n characters with repetitions allowed ; Count of palindromic strings ; Count of non - palindromic strings ; Driver Code
function power ( base , pow ) { let res = 1 ; while ( pow > 0 ) { if ( ( pow & 1 ) == 1 ) res = ( res * base ) ; base = ( base * base ) ; pow >>= 1 ; } return res ; } function countNonPalindromicString ( n , m ) { let total = power ( n , m ) ; let palindrome = power ( n , m / 2 + m % 2 ) ; let count = total - palindrome ; return count ; } let n = 3 , m = 5 ; document . write ( countNonPalindromicString ( n , m ) ) ;