Problem Statement
Given two integers n and k, consider an alphabet consisting of the first k lowercase English letters.
We need to find the number of palindromic strings whose length is less than or equal to n, subject to the following conditions:
Every character must belong to the first
klowercase English letters.No character can appear more than twice.
Return the answer modulo
10^9 + 7.
Example 1
Input:
n = 3
k = 2
Output:
6The valid strings are:
a
b
aa
bb
aba
babTherefore, the answer is 6.
Example 2
Input:
n = 4
k = 3
Output:
18The valid strings are:
a
b
c
aa
bb
cc
aba
aca
bab
bcb
cac
cbc
abba
acca
baab
bccb
caac
cbbcTherefore, the answer is 18.
Important Observation
The most important part of this problem is understanding the structure of a palindrome.
A palindrome reads the same from left to right and right to left.
For example:
aba
abba
abcbaBecause every character can appear at most twice, a character used in a palindrome can generally appear:
Twice, as a mirrored pair.
Once, only when the palindrome has odd length, in the middle.
This allows us to divide the problem into two cases:
Even-length palindromes
Odd-length palindromes
Case 1: Even-Length Palindromes
Suppose the length is:
2 * mFor example:
abbaThe characters are:
a b b aHere, a appears twice and b appears twice.
Because no character can appear more than twice, all m characters in the first half must be different.
For example, with k = 3:
a bproduces:
abbaThe first half determines the second half.
If we choose:
a, bthe palindrome is:
abbaIf we choose:
a, cthe palindrome is:
accaTherefore, for an even-length palindrome of length 2m, we need to select and arrange m distinct characters from k characters.
This is a permutation.
The number of possibilities is:
P(k, m)or:
k * (k - 1) * (k - 2) * ... * (k - m + 1)Example
For:
k = 3
m = 2we have:
P(3, 2)
= 3 * 2
= 6The six palindromes are:
abba
acca
baab
bccb
caac
cbbcCase 2: Odd-Length Palindromes
Now consider a palindrome of length:
2 * m + 1For example:
abaThe structure is:
a b aThe middle character can occur only once.
The remaining m characters form pairs.
For example:
abcbahas the structure:
a b c b aHere:
aappears twicebappears twicecappears once
The middle character can be selected in k ways.
After selecting the middle character, we need to select and arrange m different characters from the remaining k - 1 characters.
Therefore:
Count = k * P(k - 1, m)Example of Odd-Length Calculation
Suppose:
n = 5
k = 3For length 3:
m = 1Formula:
k * P(k - 1, 1)
= 3 * 2
= 6The palindromes are:
aba
aca
bab
bcb
cac
cbcFor length 5:
m = 2Formula:
3 * P(2, 2)
= 3 * 2 * 1
= 6The palindromes are:
abcba
acbca
bacab
bcacb
cabac
cbabcCombining Both Cases
We need all palindromes whose length is at most n.
Therefore, we add:
Length 1
Length 2
Length 3
Length 4
...
Length nBut instead of calculating every palindrome explicitly, we use the formulas.
For even length:
2m
Count = P(k, m)For odd length:
2m + 1
Count = k * P(k - 1, m)Java Solution
class Solution {
static final long MOD = 1000000007L;
public int palindromicStrings(int n, int k) {
long ans = 0;
// Even length: 2 * len
// Count = P(k, len)
long perm = 1;
for (int len = 1; len <= k && 2 * len <= n; len++) {
perm = (perm * (k - len + 1)) % MOD;
ans = (ans + perm) % MOD;
}
// Odd length: 2 * len + 1
// Count = k * P(k - 1, len)
perm = 1;
for (int len = 0; len <= k - 1 && 2 * len + 1 <= n; len++) {
if (len > 0) {
perm = (perm * (k - len)) % MOD;
}
long count = (k * perm) % MOD;
ans = (ans + count) % MOD;
}
return (int) ans;
}
}Detailed Code Explanation
1. Modulo Value
static final long MOD = 1000000007L;The number of possible strings can become very large.

Join the conversation! Your thoughts help the community grow.