Given a string ‘S’ and an integer ‘ p ’. You need to find if you can make ‘ p ’ number of non-empty palindromic strings by using all the characters of string ‘ S ’. Note that you can use a character only once and if there are more than once occurrence of that character you can use it that many times only.
For Example :If the given string is “ninjas” and p = 4 then we can make 4 palindromic strings as “nin” + “j” + “a” + “s”.
The first line contains ‘T’ denoting the number of test cases.
The first line of each test case contains the string ‘S’.
The second line of each test case contains the integer ‘ p ’.
Output Format :
For each test case, print “True” if it is possible to construct ‘ p ’ palindromic strings from all letters of string ‘S’ and print “False” if it is not possible.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= |S| <= 10^5
1 <= p <= 10^5
Where |S| is the length of the string ‘ S ’
Time Limit: 1sec
2
ababc
2
pqrst
3
True
False
Test Case 1: The given string is “ababc” and p = 2. We can make 2 palindromic strings from the given string as “abba” + “c”.
Test Case 2: We can not make 3 palindromic strings from the given string.
2
ydredd
5
kkkk
5
True
False
A palindromic string can have at most one character having an odd count.
Algorithm
O( |S| ), where |S| is the length of the string ‘S’.
We are traversing the string linearly for every character of ‘S’.Therefore the time complexity is O( |S| ).
O( |S| ), where |S| is the length of the string ‘S’.
We are using a hash map ‘charCount’ that stores the count of all the distinct characters. The maximum number of distinct characters can be equal to the length of the string. Therefore space complexity is of order O( |S| ).