Given an integer K and a string str of lowercase English characters, the task is to find a string s such that when s is repeated exactly K times, it gives a permutation of S. If no such string exists, print -1.
Input: str = “aabb”, k = 2
“ab” when repeated 2 times gives “abab” which is a permutation of “aabb”
Input: str = “aabb”, k = 3
Approach: An efficient approach is to count the frequency of each character of the given string. If the frequency of any character is not divisible by k then the solution is not possible and print -1. Otherwise, add every character (frequency / k) times to the resultant string and print the generated string in the end.
Below is the implementation of the above approach:
- Check if any permutation of string is a K times repeated string
- Longest sub string of 0's in a binary string which is repeated K times
- Largest element in the array that is repeated exactly k times
- Smallest String consisting of a String S exactly K times as a Substring
- Count of subarrays which contains a given number exactly K times
- Number of subsequences as "ab" in a string repeated K times
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Remove characters from a String that appears exactly K times
- Minimum number of times A has to be repeated such that B is a substring of it
- Number formed after K times repeated addition of smallest divisor of N
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Count words that appear exactly two times in an array of words
- Count of Numbers in a Range where digit d occurs exactly K times
- Number of sub-strings that contain the given character exactly k times
- Find Nth smallest number that is divisible by 100 exactly K times
- Check if original Array is retained after performing XOR with M exactly K times
- Lexicographically smallest string which differs from given strings at exactly K indices
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.