Count of non-palindromic strings of length M using given N characters
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 character can be used more than once.
Examples:
Input: N = 3, M = 2
Output: 6
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++
#include <bits/stdc++.h>
using namespace std;
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;
}
unsigned long long countNonPalindromicString(
unsigned long long n,
unsigned long long m)
{
unsigned long long total
= power(n, m);
unsigned long long palindrome
= power(n, m / 2 + m % 2);
unsigned long long count
= total - palindrome;
return count;
}
int main()
{
int n = 3, m = 5;
cout<< countNonPalindromicString(n, m);
return 0;
}
|
Java
import java.util.*;
class GFG{
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;
}
static long countNonPalindromicString( long n,
long m)
{
long total = power(n, m);
long palindrome = power(n, m / 2 + m % 2 );
long count = total - palindrome;
return count;
}
public static void main(String[] args)
{
int n = 3 , m = 5 ;
System.out.println(
countNonPalindromicString(n, m));
}
}
|
Python3
def power(base, pwr):
res = 1
while (pwr > 0 ):
if (pwr & 1 ):
res = res * base
base = base * base
pwr >> = 1
return res
def countNonPalindromicString(n, m):
total = power(n, m)
palindrome = power(n, m / / 2 + m % 2 )
count = total - palindrome
return count
if __name__ = = '__main__' :
n = 3
m = 5
print (countNonPalindromicString(n, m))
|
C#
using System;
class GFG{
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;
}
static long countNonPalindromicString( long n,
long m)
{
long total = power(n, m);
long palindrome = power(n, m / 2 + m % 2);
long count = total - palindrome;
return count;
}
public static void Main(String[] args)
{
int n = 3, m = 5;
Console.WriteLine(
countNonPalindromicString(n, m));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(log(M))
Auxiliary Space: O(1)
Efficient Approach:
- We need to count the number of non-palindromic strings of length M using N distinct characters
- The total number of strings of length M using N distinct characters is N^M.
- We need to subtract the number of palindromic strings of length M from the above result to get the count of non-palindromic strings.
- For a given length M, we can calculate the number of palindromic strings by counting the number of strings that can be formed using the first M/2 characters and then squaring the result.
- If M is odd, then we multiply the result by N as the middle character can be any of the N characters.
- Subtracting the number of palindromic strings from N^M will give us the required count of non-palindromic strings.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
long long countNonPalindromicStrings( int N, int M) {
long long countPalindromicStrings = 0;
if (M % 2 == 0) {
countPalindromicStrings = pow (N, M/2);
}
else {
countPalindromicStrings = pow (N, (M-1)/2) * N;
}
return pow (N, M) - countPalindromicStrings;
}
int main() {
int N = 3, M = 2;
cout << countNonPalindromicStrings(N, M) << endl;
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = 3 ;
int M = 2 ;
long result = countNonPalindromicStrings(N, M);
System.out.println(result);
}
public static long countNonPalindromicStrings( int N, int M) {
long countPalindromicStrings = 0 ;
if (M % 2 == 0 ) {
countPalindromicStrings = ( long ) Math.pow(N, M / 2 );
} else {
countPalindromicStrings = ( long ) (Math.pow(N, (M - 1 ) / 2 ) * N);
}
return ( long ) Math.pow(N, M) - countPalindromicStrings;
}
}
|
Python
def count_non_palindromic_strings(N, M):
count_palindromic_strings = 0
if M % 2 = = 0 :
count_palindromic_strings = N * * (M / / 2 )
else :
count_palindromic_strings = N * * ((M - 1 ) / / 2 ) * N
return N * * M - count_palindromic_strings
def main():
N, M = 3 , 2
print (count_non_palindromic_strings(N, M))
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG {
static long CountNonPalindromicStrings( int N, int M)
{
long countPalindromicStrings = 0;
if (M % 2 == 0) {
countPalindromicStrings
= ( long )Math.Pow(N, M / 2);
}
else {
countPalindromicStrings
= ( long )Math.Pow(N, (M - 1) / 2) * N;
}
return ( long )Math.Pow(N, M)
- countPalindromicStrings;
}
static void Main( string [] args)
{
int N = 3, M = 2;
Console.WriteLine(CountNonPalindromicStrings(
N, M));
}
}
|
Javascript
function countNonPalindromicString(N, M) {
let countPalindromicStrings = 0;
if (M % 2 === 0) {
countPalindromicStrings = Math.pow(N, M / 2);
} else {
countPalindromicStrings = Math.pow(N, (M - 1) / 2) * N;
}
return Math.pow(N, M) - countPalindromicStrings;
}
const N = 3, M = 2;
console.log(countNonPalindromicString(N, M));
|
Time Complexity: O(log M).
Auxiliary Space: O(1), No Extra space is being used.
Last Updated :
28 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...