Count alphanumeric palindromes of length N
Given a positive integer N, the task is to find the number of alphanumeric palindromic strings of length N. Since the count of such strings can be very large, print the answer modulo 109 + 7.
Examples:
Input: N = 2
Output: 62
Explanation: There are 26 palindromes of the form {“AA”, “BB”, …, “ZZ”}, 26 palindromes of the form {“aa”, “bb”, …, “cc”} and 10 palindromes of the form {“00”, “11”, …, “99”}. Therefore, the total number of palindromic strings = 26 + 26 + 10 = 62.
Input: N = 3
Output: 3844
Naive Approach: The simplest approach is to generate all possible alphanumeric strings of length N and for each string, check if it is a palindrome or not. Since, at each position, 62 characters can be placed in total. Hence, there are 62N possible strings.
Time Complexity: O(N*62N)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use the property of palindrome. It can be observed that if the length of the string is even, then for each index i from 0 to N/2, characters at indices i and (N – 1 – i) are the same. Therefore, for each position from 0 to N/ 2 there are 62N/2 options. Similarly, if the length is odd, 62(N+1)/2 options are there. Hence, it can be said that, for some N, there are 62ceil(N/2) possible palindromic strings.
Follow the steps below to solve the problem:
- For the given value of N, calculate 62ceil(N/2) mod 109 + 7 using Modular Exponentiation.
- Print 62ceil(N/2) mod 109 + 7 as the required answer.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int power( int x, int y,
int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int main()
{
int N = 3;
int flag, k, m;
if ((N == 1) ||
(N == 2))
cout << 62;
else
m = 1000000000 + 7;
if (N % 2 == 0)
{
k = N / 2;
flag = true ;
}
else
{
k = (N - 1) / 2;
flag = false ;
}
if (flag != 0)
{
int a = power(62,
k, m);
cout << a;
}
else
{
int a = power(62,
(k + 1), m);
cout << a;
}
}
|
Java
import java.util.*;
class GFG{
static int power( int x,
int y, int p)
{
int res = 1 ;
x = x % p;
if (x == 0 )
return 0 ;
while (y > 0 )
{
if ((y & 1 ) == 1 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
public static void main(String[] args)
{
int N = 3 ;
int flag, k, m = 0 ;
if ((N == 1 ) || (N == 2 ))
System.out.print( 62 );
else
m = 1000000000 + 7 ;
if (N % 2 == 0 )
{
k = N / 2 ;
flag = 1 ;
}
else
{
k = (N - 1 ) / 2 ;
flag = 0 ;
}
if (flag != 0 )
{
int a = power( 62 , k, m);
System.out.print(a);
}
else
{
int a = power( 62 , (k + 1 ), m);
System.out.print(a);
}
}
}
|
Python3
def power(x, y, p):
res = 1
x = x % p
if (x = = 0 ):
return 0
while (y > 0 ):
if ((y & 1 ) = = 1 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
N = 3
if ((N = = 1 ) or (N = = 2 )):
print ( 62 )
else :
m = ( 10 * * 9 ) + 7
if (N % 2 = = 0 ):
k = N / / 2
flag = True
else :
k = (N - 1 ) / / 2
flag = False
if (flag):
a = power( 62 , k, m)
print (a)
else :
a = power( 62 , (k + 1 ), m)
print (a)
|
C#
using System;
class GFG{
static int power( int x, int y,
int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static void Main()
{
int N = 3;
int flag, k, m = 0;
if ((N == 1) || (N == 2))
Console.Write(62);
else
m = 1000000000 + 7;
if (N % 2 == 0)
{
k = N / 2;
flag = 1;
}
else
{
k = (N - 1) / 2;
flag = 0;
}
if (flag != 0)
{
int a = power(62, k, m);
Console.Write(a);
}
else
{
int a = power(62, (k + 1), m);
Console.Write(a);
}
}
}
|
Javascript
<script>
function power(x, y, p)
{
let res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
let N = 3;
let flag, k, m =0;
if ((N == 1) || (N == 2))
document.write(62);
else
m = 1000000000 + 7;
if (N % 2 == 0)
{
k = N / 2;
flag = 1;
}
else
{
k = (N - 1) / 2;
flag = 0;
}
if (flag != 0)
{
let a = power(62, k, m);
document.write(a);
}
else
{
let a = power(62, (k + 1), m);
document.write(a);
}
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(N)
Last Updated :
26 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...