# Program to count number of palindromes of size k can be formed from the given string characters in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a string s that is representing alphabet characters and a number k. We have to find the number of palindromes where we can construct of length k using only letters in s. And we can use these letters more than once if we want.

So, if the input is like s = "xy", k = 4, then the output will be 4 as the palindromes are [xxxx, yyyy, xyyx, yxxy].

To solve this, we will follow these steps −

• n := quotient of k/2
• x := number of unique characters in s
• return x^(n + k mod 2)

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def solve(self, s, k):
n=k//2
return len(set(s))**(n+k%2)
s = "xy"
k = 4
ob = Solution()
print(ob.solve(s, k))

## Input

"xy",4

## Output

4