- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

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

- Related Questions & Answers
- Program to count number of unique palindromes we can make using string characters in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Check whether second string can be formed from characters of first string in Python
- Counting the number of palindromes that can be constructed from a string in JavaScript
- C++ Program to count number of teams can be formed for coding challenge
- Program to count number of palindromes after minimum number of split of the string in C++
- C++ program to find number of groups can be formed from set of programmers
- Program to find length of longest word that can be formed from given letters in python
- Python Program to Count Number of Lowercase Characters in a String
- Check if given string can be formed by concatenating string elements of list in Python
- Check if a string can be formed from another string using given constraints in Python
- Print all possible strings of length k that can be formed from a set of n characters in C++
- All possible strings of any length that can be formed from a given string?
- C++ Program to count number of characters to be removed to get good string
- Count Number of Lowercase Characters in a String in Python Program

Advertisements