Number of palindromic strings
Medium Accuracy: 0.0% Submissions: 0 Points: 4

Given two integers N and K, the task is to find the count of palindromic strings of length lesser than or equal to N, with first K characters of lowercase English language, such that each character in a string doesn’t appear more than twice.

Note: Anwer can be very large, so, output answer modulo 109+7

Example 1:

Input: N = 3, K = 2
Output: 6
Explanation: The possible strings are:
"a", "b", "aa", "bb", "aba", "bab".

Example 2:

Input: N = 4, K = 3
Output: 6
Explanation: The possible strings are: 
"a", "b", "c", "aa", "bb", "cc", "aba",
"aca", "bab", "bcb", "cac", "cbc", 
"abba", "acca", "baab", "bccb", "caac", 

Your task:
You do not need to read any input or print anything. The task is to complete the function palindromicStrings(), which takes two integers as input and returns the count.

Expected Time Complexity: O(K2)
Expected Auxiliary Space: O(K2)

1 ≤ K ≤ 26
1 ≤ N ≤ 52
N ≤ 2*K

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Number of palindromic strings

Output Window