- 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 if given string can be formed by concatenating string elements of list in Python
- All possible strings of any length that can be formed from a given string?
- Print all possible strings of length k that can be formed from a set of n characters in C++
- Program to count number of unique binary search tree can be formed with 0 to n values in Python
- Program to find possible number of palindromes we can make by trimming string in Python
- Find maximum number that can be formed using digits of a given number in C++
- Find the count of sub-strings whose characters can be rearranged to form the given word in Python
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Count the Number of matching characters in a pair of string in Python
- Find all strings formed from characters mapped to digits of a number in Python
- Python program to count number of vowels using set in a given string
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++

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

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 −

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))

"xy",4

4

Advertisements