Given two positive integers **N** and **M**, the task is to calculate the number of non-palindromic strings of length **M** using given **N** distinct characters. **Note:** Each distinct characters can be used more than once.

**Examples:**

Input:N = 3, M = 2Output:6Explanation:

Since only 3 characters are given, those 3 characters can be used to form3different strings. Out of these, only 3 strings are palindromic. Hence, the remaining 6 strings are palindromic.^{2}

Input:N = 26, M = 5Output:11863800

**Approach:**

Follow the steps below to solve the problem:

- Total number of strings of length
**M**using given**N**characters will be**N**.^{M} - For a string to be a palindrome, the first half and second half should be equal. For
**even**values of**M**, we need to select only**M/2**characters from the given N characters. For**odd**values, we need to select**M/2 + 1**characters from the given N characters. Since repetitions are allowed, the total number of palindromic strings of length**M**will be**N**.^{(M/2 + M%2)} - The required count of non-palindromic strings is given by the following equation:
**N**^{M}- N^{(M/2 + M%2)}

Below is the implementation of the above approach:

## C++

`// C++ Program to count ` `// non-palindromic strings ` `// of length M using N ` `// distinct characters ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` ` ` `// Iterative Function to calculate ` `// base^pow in O(log y) ` `unsigned ` `long` `long` `power( ` ` ` `unsigned ` `long` `long` `base, ` ` ` `unsigned ` `long` `long` `pow` `) ` `{ ` ` ` `unsigned ` `long` `long` `res = 1; ` ` ` `while` `(` `pow` `> 0) { ` ` ` `if` `(` `pow` `& 1) ` ` ` `res = (res * base); ` ` ` `base = (base * base); ` ` ` `pow` `>>= 1; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to return the ` `// count of non palindromic strings ` `unsigned ` `long` `long` `countNonPalindromicString( ` ` ` `unsigned ` `long` `long` `n, ` ` ` `unsigned ` `long` `long` `m) ` `{ ` ` ` `// Count of strings using n ` ` ` `// characters with ` ` ` `// repetitions allowed ` ` ` `unsigned ` `long` `long` `total ` ` ` `= power(n, m); ` ` ` ` ` `// Count of palindromic strings ` ` ` `unsigned ` `long` `long` `palindrome ` ` ` `= power(n, m / 2 + m % 2); ` ` ` ` ` `// Count of non-palindromic strings ` ` ` `unsigned ` `long` `long` `count ` ` ` `= total - palindrome; ` ` ` ` ` `return` `count; ` `} ` `int` `main() ` `{ ` ` ` ` ` `int` `n = 3, m = 5; ` ` ` `cout<< countNonPalindromicString(n, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count non-palindromic ` `// strings of length M using N distinct ` `// characters ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Iterative Function to calculate ` `// base^pow in O(log y) ` `static` `long` `power(` `long` `base, ` `long` `pow) ` `{ ` ` ` `long` `res = ` `1` `; ` ` ` `while` `(pow > ` `0` `) ` ` ` `{ ` ` ` `if` `((pow & ` `1` `) == ` `1` `) ` ` ` `res = (res * base); ` ` ` `base = (base * base); ` ` ` `pow >>= ` `1` `; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to return the ` `// count of non palindromic strings ` `static` `long` `countNonPalindromicString(` `long` `n, ` ` ` `long` `m) ` `{ ` ` ` ` ` `// Count of strings using n ` ` ` `// characters with ` ` ` `// repetitions allowed ` ` ` `long` `total = power(n, m); ` ` ` ` ` `// Count of palindromic strings ` ` ` `long` `palindrome = power(n, m / ` `2` `+ m % ` `2` `); ` ` ` ` ` `// Count of non-palindromic strings ` ` ` `long` `count = total - palindrome; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `3` `, m = ` `5` `; ` ` ` ` ` `System.out.println( ` ` ` `countNonPalindromicString(n, m)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count non-palindromic strings ` `# of length M using N distinct characters ` ` ` `# Iterative Function to calculate ` `# base^pow in O(log y) ` `def` `power(base, pwr): ` ` ` ` ` `res ` `=` `1` ` ` `while` `(pwr > ` `0` `): ` ` ` `if` `(pwr & ` `1` `): ` ` ` `res ` `=` `res ` `*` `base ` ` ` `base ` `=` `base ` `*` `base ` ` ` `pwr >>` `=` `1` ` ` ` ` `return` `res ` ` ` `# Function to return the count ` `# of non palindromic strings ` `def` `countNonPalindromicString(n, m): ` ` ` ` ` `# Count of strings using n ` ` ` `# characters with ` ` ` `# repetitions allowed ` ` ` `total ` `=` `power(n, m) ` ` ` ` ` `# Count of palindromic strings ` ` ` `palindrome ` `=` `power(n, m ` `/` `/` `2` `+` `m ` `%` `2` `) ` ` ` ` ` `# Count of non-palindromic strings ` ` ` `count ` `=` `total ` `-` `palindrome ` ` ` ` ` `return` `count ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `3` ` ` `m ` `=` `5` ` ` ` ` `print` `(countNonPalindromicString(n, m)) ` ` ` `# This code is contributed by Shivam Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count non-palindromic ` `// strings of length M using N distinct ` `// characters ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Iterative Function to calculate ` `// base^pow in O(log y) ` `static` `long` `power(` `long` `Base, ` `long` `pow) ` `{ ` ` ` `long` `res = 1; ` ` ` `while` `(pow > 0) ` ` ` `{ ` ` ` `if` `((pow & 1) == 1) ` ` ` `res = (res * Base); ` ` ` ` ` `Base = (Base * Base); ` ` ` `pow >>= 1; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to return the ` `// count of non palindromic strings ` `static` `long` `countNonPalindromicString(` `long` `n, ` ` ` `long` `m) ` `{ ` ` ` ` ` `// Count of strings using n ` ` ` `// characters with ` ` ` `// repetitions allowed ` ` ` `long` `total = power(n, m); ` ` ` ` ` `// Count of palindromic strings ` ` ` `long` `palindrome = power(n, m / 2 + m % 2); ` ` ` ` ` `// Count of non-palindromic strings ` ` ` `long` `count = total - palindrome; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 3, m = 5; ` ` ` ` ` `Console.WriteLine( ` ` ` `countNonPalindromicString(n, m)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

216

**Time Complexity:** O(log(N))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum count of sub-strings of length K consisting of same characters
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Print all possible strings of length k that can be formed from a set of n characters
- Count of same length Strings that exists lexicographically in between two given Strings
- Total length of string from given Array of strings composed using given characters
- Maximize length of the String by concatenating characters from an Array of Strings
- Count of binary strings of length N having equal count of 0's and 1's
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count common characters in two strings
- Count of sub-strings that do not contain all the characters from the set {'a', 'b', 'c'} at the same time
- Count of substrings of length K with exactly K distinct characters
- Count of strings where adjacent characters are of difference one
- Count the Number of matching characters in a pair of strings
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Count of sub-strings of length n possible from the given string
- Find the count of sub-strings whose characters can be rearranged to form the given word
- Count of Distinct strings possible by inserting K characters in the original string
- Count the number of strings in an array whose distinct characters are less than equal to M
- Sort an array of strings based on count of distinct characters
- Count number of binary strings of length N having only 0's and 1's

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.