Related Articles

# Count of non-palindromic strings of length M using given N characters

• Last Updated : 07 May, 2021

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 = 2
Output:
Explanation:
Since only 3 characters are given, those 3 characters can be used to form 32 different strings. Out of these, only 3 strings are palindromic. Hence, the remaining 6 strings are palindromic.

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

Approach:
Follow the steps below to solve the problem:

• Total number of strings of length M using given N characters will be NM.
• 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:
`NM - 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 ``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;``}`

## 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`

## 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`

## 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`

## Javascript

 ``
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up