Given a string S and an integer K, the task is to check if it is possible to distribute these characters into two strings such that count of characters having a frequency K in both strings is equal.
If it is possible, then print a sequence consisting of 1 and 2, which denotes which character should be placed in which string. Otherwise, print NO.
Note: One of these new strings can be empty.
Input: S = “abbbccc”, K = 1
The two strings are “abbbcc” and “c”.
Hence, both the strings have exactly 1 character having frequency K( = 1).
Input: S = “aaaa”, K = 3
String can be split into “aaaa” and “”.
Hence, no character has frequency 3 in both the strings.
Follow the steps below to solve the problem:
Check for the following three conditions to determine if a split is possible or not:
- If the total number of characters having a frequency K in the initial string is even, then these characters can be placed equally into two strings and the rest of the characters(having a frequency not equal to K) can be placed in any of the two groups.
If the total number of characters having a frequency K in the initial string is odd, then if there is a character in the initial string having a frequency greater than K but not equal to 2K, then such a distribution is possible.
S =”abceeee”, K = 1
Split into “abeee” and “ce”. Hence, both the strings have 2 characters with frequency 1.
If the total number of characters having a frequency K in the initial string is odd, then if there is a character in the initial string having a frequency equal to 2K, then such distribution is possible.
S =”aaaabbccdde”, K = 2
Split into “aabbc” and “aaddce” so that both the strings have two characters with frequency 2.
- If all the three condition mentioned above fails then answer is “NO”.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a string can be split into substrings starting with N followed by N characters
- Check if given string can be split into four distinct strings
- Find the number of strings formed using distinct characters of a given string
- Check if string remains palindrome after removing given number of characters
- Remove minimum characters from string to split it into three substrings under given constraints
- Check whether two strings contain same characters in same order
- Meta Strings (Check if two strings can become same after a swap in one string)
- Concatenated string with uncommon characters of two strings
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Check if a string can be split into even length palindromic substrings
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Count of Distinct strings possible by inserting K characters in the original string
- Maximize length of the String by concatenating characters from an Array of Strings
- Total length of string from given Array of strings composed using given characters
- Check whether second string can be formed from characters of first string
- Count the Number of matching characters in a pair of strings
- Remove minimum number of characters so that two strings become anagram
- Check if both halves of the string have same set of characters
- Quick way to check if all the characters of a string are same
- Split the binary string into substrings with equal number of 0s and 1s
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.
Improved By : mohit kumar 29