由最多出现两次的前 K 个字母组成的大小为 N 的回文字符串的计数
给定两个整数N和K ,任务是找到由前K个小写字母组成的大小为 N的回文字符串的数量,使得字符串中的每个字符出现的次数不超过两次。
例子:
Input: N = 3, K = 2
Output: 6
Explanation:
The possible strings are:
“a”, “b”, “aa”, “bb”, “aba”, “bab”.
Input: N = 4, K = 3
Output: 18
Explanation:
The possible strings are:
“a”, “b”, “c”, “aa”, “bb”, “cc”, “aba”, “aca”, “bab”, “bcb”, “cac”, “cbc”, “abba”, “acca”, “baab”, “bccb”, “caac”, “cbbc”.
方法:可以根据以下观察解决给定的问题:
- 让我们尝试构建一个只有前 3 个英文字母( K = 3 )的 4 位回文( N = 4 )。所以,这个想法是创建一个空字符串( _ _ _ _ ),现在要从中得到一个回文,只能填充一半中的两位数,因为其他两位将根据它们,即,如果选择前两个位置来填充所选字符,则后两个位置将与该字符相同,因此字符串应该是回文。
- 在这种情况下,如果a填在第一个位置, b填在第二个位置,那么剩下的第三个和第四个唯一的选择就是分别用b和a填充。
- 所以这意味着,要找到长度为 4( N = 4 )且只有前 3 个字母( K = 3 )的回文字符串的数量,计算前 2 个数字(= N/2 )的所有可能组合,即3 *2=6 (第一个位置 3 个选项,第二个位置 2 个选项)。
- 上面解释的情况是对于一个偶数长度的字符串( N 是偶数),对于一个奇数长度的字符串( N 是奇数),可以填充N/2 + 1 个索引。
请按照以下步骤解决给定的问题:
- 找到长度最多为 N个的回文字符串,然后从1 到 N计数每个长度的回文字符串,然后将它们相加。
- 对于N的值为:
- 如果 N 是偶数,则找到所有可能的组合,直到N/2 ,因为只有一半的位置可以被填充。
- 如果 N 是奇数,则找到所有可能的组合,直到N/2 +1 ,以及作为中间元素的元素的额外 + 1 。
- 将所有这些加在一起并相应地打印答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function of return the number of
// palindromic strings of length N with
// first K alphabets possible
int lengthNPalindrome(int N, int K)
{
int half = N / 2;
// If N is odd, half + 1 position
// can be filled to cope with the
// extra middle element
if (N & 1) {
half += 1;
}
int ans = 1;
for (int i = 1; i <= half; i++) {
ans *= K;
// K is reduced by one, because
// count of choices for the next
// position is reduced by 1 as
// a element can only once
K--;
}
// Return the possible count
return ans;
}
// Function to find the count of palindromic
// string of first K characters according
// to the given criteria
int palindromicStrings(int N, int K)
{
// If N=1, then only K palindromic
// strings possible.
if (N == 1) {
return K;
}
// If N=2, the 2*K palindromic strings
// possible, K for N=1 and K for N=2
if (N == 2) {
return 2 * K;
}
int ans = 0;
// Initialize ans with the count of
// strings possible till N = 2
ans += (2 * K);
for (int i = 3; i <= N; i++) {
ans += lengthNPalindrome(i, K);
}
// Return the possible count
return ans;
}
// Driver Code
int main()
{
int N = 4, K = 3;
cout << palindromicStrings(N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function of return the number of
// palindromic strings of length N with
// first K alphabets possible
static int lengthNPalindrome(int N, int K)
{
int half = N / 2;
// If N is odd, half + 1 position
// can be filled to cope with the
// extra middle element
if (N % 2 == 1) {
half += 1;
}
int ans = 1;
for (int i = 1; i <= half; i++) {
ans *= K;
// K is reduced by one, because
// count of choices for the next
// position is reduced by 1 as
// a element can only once
K--;
}
// Return the possible count
return ans;
}
// Function to find the count of palindromic
// string of first K characters according
// to the given criteria
static int palindromicStrings(int N, int K)
{
// If N=1, then only K palindromic
// strings possible.
if (N == 1) {
return K;
}
// If N=2, the 2*K palindromic strings
// possible, K for N=1 and K for N=2
if (N == 2) {
return 2 * K;
}
int ans = 0;
// Initialize ans with the count of
// strings possible till N = 2
ans += (2 * K);
for (int i = 3; i <= N; i++) {
ans += lengthNPalindrome(i, K);
}
// Return the possible count
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 4, K = 3;
System.out.println(palindromicStrings(N, K));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 program for the above approach
# Function of return the number of
# palindromic strings of length N with
# first K alphabets possible
def lengthNPalindrome(N, K) :
half = N // 2;
# If N is odd, half + 1 position
# can be filled to cope with the
# extra middle element
if (N & 1) :
half += 1;
ans = 1;
for i in range(1, half + 1) :
ans *= K;
# K is reduced by one, because
# count of choices for the next
# position is reduced by 1 as
# a element can only once
K -= 1;
# Return the possible count
return ans;
# Function to find the count of palindromic
# string of first K characters according
# to the given criteria
def palindromicStrings(N, K) :
# If N=1, then only K palindromic
# strings possible.
if (N == 1) :
return K;
# If N=2, the 2*K palindromic strings
# possible, K for N=1 and K for N=2
if (N == 2) :
return 2 * K;
ans = 0;
# Initialize ans with the count of
# strings possible till N = 2
ans += (2 * K);
for i in range(3, N + 1) :
ans += lengthNPalindrome(i, K);
# Return the possible count
return ans;
# Driver Code
if __name__ == "__main__" :
N = 4; K = 3;
print(palindromicStrings(N, K));
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
class GFG
{
// Function of return the number of
// palindromic strings of length N with
// first K alphabets possible
static int lengthNPalindrome(int N, int K)
{
int half = N / 2;
// If N is odd, half + 1 position
// can be filled to cope with the
// extra middle element
if (N % 2 == 1) {
half += 1;
}
int ans = 1;
for (int i = 1; i <= half; i++) {
ans *= K;
// K is reduced by one, because
// count of choices for the next
// position is reduced by 1 as
// a element can only once
K--;
}
// Return the possible count
return ans;
}
// Function to find the count of palindromic
// string of first K characters according
// to the given criteria
static int palindromicStrings(int N, int K)
{
// If N=1, then only K palindromic
// strings possible.
if (N == 1) {
return K;
}
// If N=2, the 2*K palindromic strings
// possible, K for N=1 and K for N=2
if (N == 2) {
return 2 * K;
}
int ans = 0;
// Initialize ans with the count of
// strings possible till N = 2
ans += (2 * K);
for (int i = 3; i <= N; i++) {
ans += lengthNPalindrome(i, K);
}
// Return the possible count
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int N = 4, K = 3;
Console.Write(palindromicStrings(N, K));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
18
时间复杂度: O(N 2 )
辅助空间: O(1)