给定一个正整数N ,任务是找到长度为N的字母数字回文字符串的数量。由于此类字符串的数量可能非常大,因此打印答案模10 9 + 7 。
例子:
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
朴素的方法:最简单的方法是生成所有可能的长度为 N 的字母数字字符串,对于每个字符串,检查它是否是回文。因为,在每个位置,总共可以放置62 个字符。因此,有62 N 个可能的字符串。
时间复杂度: O(N*62 N )
辅助空间: O(N)
高效的方法:优化上述方法,其思想是利用回文的特性。可以观察到,如果字符串的长度为偶数,那么对于从0 到 N/2 的每个索引i ,索引i和(N – 1 – i)处的字符是相同的。因此,对于从0到N/2 的每个位置,有62 个N/2选项。同样,如果长度是奇数,则有62 (N+1)/2 个选项。因此,可以说,对于某些N ,有62 个ceil(N/2)可能的回文字符串。
请按照以下步骤解决问题:
- 对于给定的N值,使用模幂运算计算62 ceil(N/2) mod 10 9 + 7 。
- 打印62 ceil(N/2) mod 10 9 + 7作为所需答案。
下面是上述方法的实现:
C++14
// C++ program for the
// above approach
#include
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
3844
时间复杂度: O(log N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live