Sunday, November 17, 2024
Google search engine
HomeData Modelling & AICount alphanumeric palindromes of length N

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




// C++ program for the
// above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate
// (x ^ y) mod p
int power(int x, int y,
          int p)
{
  // Initialize result
  int res = 1;
 
  // Update x if it is more
  // than or equal to p
  x = x % p;
 
  if (x == 0)
    return 0;
 
  while (y > 0)
  {
    // If y is odd, multiply
    // x with result
    if ((y & 1) == 1)
      res = (res * x) % p;
 
    // y must be even now
    y = y >> 1;
    x = (x * x) % p;
  }
 
  // Return the final
  // result
  return res;
}
 
// Driver Code
int main()
{   
  // Given N
  int N = 3;
  int flag, k, m;
 
  // Base Case
  if((N == 1) ||
     (N == 2))
    cout << 62;
 
  else
    m = 1000000000 + 7;
 
  // Check whether n
  // is even or odd
  if(N % 2 == 0)
  {
    k = N / 2;
    flag = true;
  }
  else
  {
    k = (N - 1) / 2;
    flag = false;
  }
  if(flag != 0)
  {      
    // Function Call
    int a = power(62,
                  k, m);
    cout << a;
  }
  else
  {
    // Function Call
    int a = power(62,
                  (k + 1), m);
    cout << a;
  }
}
 
// This code is contributed by sanjoy_62


Java




// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function to calculate
// (x ^ y) mod p
static int power(int x,
                 int y, int p)
{
  // Initialize result
  int res = 1;
 
  // Update x if it is more
  // than or equal to p
  x = x % p;
 
  if (x == 0)
    return 0;
 
  while (y > 0)
  {
    // If y is odd, multiply
    // x with result
    if ((y & 1) == 1)
      res = (res * x) % p;
 
    // y must be even now
    y = y >> 1;
    x = (x * x) % p;
  }
 
  // Return the final
  // result
  return res;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given N
  int N = 3;
  int flag, k, m =0;
 
  // Base Case
  if ((N == 1) || (N == 2))
    System.out.print(62);
  else
    m = 1000000000 + 7;
 
  // Check whether n
  // is even or odd
  if (N % 2 == 0)
  {
    k = N / 2;
    flag = 1;
  }
  else
  {
    k = (N - 1) / 2;
    flag = 0;
  }
   
  if (flag != 0)
  {
    // Function Call
    int a = power(62, k, m);
    System.out.print(a);
  }
  else
  {
    // Function Call
    int a = power(62, (k + 1), m);
    System.out.print(a);
  }
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 program for the above approach
 
# Function to calculate (x ^ y) mod p
def power(x, y, p):
 
    # Initialize result
    res = 1
 
    # Update x if it is more
    # than or equal to p
    x = x % p
 
    if (x == 0):
        return 0
 
    while (y > 0):
 
        # If y is odd, multiply
        # x with result
        if ((y & 1) == 1):
            res = (res * x) % p
 
        # y must be even now
        y = y >> 1
        x = (x * x) % p
 
    # Return the final result
    return res
 
 
# Driver Code
 
# Given N
N = 3
 
# Base Case
if((N == 1) or (N == 2)):
    print(62)
     
else:
 
    m = (10**9)+7
 
    # Check whether n
    # is even or odd
    if(N % 2 == 0):
        k = N//2
        flag = True
    else:
        k = (N - 1)//2
        flag = False
         
    if(flag):
       
        # Function Call
        a = power(62, k, m)
        print(a)
    else:
 
        # Function Call
        a = power(62, (k + 1), m)
        print(a)


C#




// C# program for the
// above approach
using System;
 
class GFG{
 
// Function to calculate
// (x ^ y) mod p
static int power(int x, int y,
                 int p)
{
   
  // Initialize result
  int res = 1;
 
  // Update x if it is more
  // than or equal to p
  x = x % p;
 
  if (x == 0)
    return 0;
 
  while (y > 0)
  {
     
    // If y is odd, multiply
    // x with result
    if ((y & 1) == 1)
      res = (res * x) % p;
 
    // y must be even now
    y = y >> 1;
    x = (x * x) % p;
  }
 
  // Return the final
  // result
  return res;
}
 
// Driver Code
public static void Main()
{
   
  // Given N
  int N = 3;
  int flag, k, m = 0;
 
  // Base Case
  if ((N == 1) || (N == 2))
    Console.Write(62);
  else
    m = 1000000000 + 7;
 
  // Check whether n
  // is even or odd
  if (N % 2 == 0)
  {
    k = N / 2;
    flag = 1;
  }
  else
  {
    k = (N - 1) / 2;
    flag = 0;
  }
   
  if (flag != 0)
  {
    // Function Call
    int a = power(62, k, m);
    Console.Write(a);
  }
  else
  {
     
    // Function Call
    int a = power(62, (k + 1), m);
    Console.Write(a);
  }
}
}
 
// This code is contributed by code_hunt


Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to calculate
// (x ^ y) mod p
function power(x, y, p)
{
  // Initialize result
  let res = 1;
  
  // Update x if it is more
  // than or equal to p
  x = x % p;
  
  if (x == 0)
    return 0;
  
  while (y > 0)
  {
    // If y is odd, multiply
    // x with result
    if ((y & 1) == 1)
      res = (res * x) % p;
  
    // y must be even now
    y = y >> 1;
    x = (x * x) % p;
  }
  
  // Return the final
  // result
  return res;
}
// Driver Code
 
      // Given N
  let N = 3;
  let flag, k, m =0;
  
  // Base Case
  if ((N == 1) || (N == 2))
    document.write(62);
  else
    m = 1000000000 + 7;
  
  // Check whether n
  // is even or odd
  if (N % 2 == 0)
  {
    k = N / 2;
    flag = 1;
  }
  else
  {
    k = (N - 1) / 2;
    flag = 0;
  }
    
  if (flag != 0)
  {
    // Function Call
    let a = power(62, k, m);
    document.write(a);
  }
  else
  {
    // Function Call
    let a = power(62, (k + 1), m);
    document.write(a);
  }
           
</script>


Output: 

3844

 

Time Complexity: O(log N)
Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments