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 palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- 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
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check whether two strings contain same characters in same order
- Check if all strings of an array can be made same by interchanging characters
- Check if a string can be split into even length palindromic substrings
- Remove minimum characters from string to split it into three substrings under given constraints
- Check if an array can be split into subarrays with GCD exceeding K
- Check if an array can be split into subsets of K consecutive elements
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Check if characters of a given string can be used to form any N equal strings
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Count of ways to split given string into two non-empty palindromes
- Check if frequency of all characters can become same by one removal
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Count of same length Strings that exists lexicographically in between two given Strings
- Split the string into minimum parts such that each part is in the another string
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.