Given a string of lowercase English alphabets and an integer 0 < K <= 26. The task is to divide the string into two parts (also print them) such that both parts have at least k different characters. If there are more than one answers possible, print one having the smallest left part. If there is no such answers, print “Not Possible”.
Input : str = “geeksforgeeks”, k = 4
Output : geeks , forgeeks
The string can be divided into two parts as “geeks” and “forgeeks”. Since “geeks” has four different characters ‘g’, ‘e’, ‘k’ and ‘s’ and this is the smallest left part, “forgeeks” has also at least four different characters.
Input : str = “aaaabbbb”, k = 2
Output :Not Possible
- Idea is to count the number of distinct characters using a Hashmap.
- If the count of the distinct variable becomes equal to k, then the left part of the string is found so store this index, break the loop and unmark all the characters.
- Now run a loop from where the left string ends to end of the given string and repeat the same process as it was done to find the left string.
- If count is greater than or equal to k, then right string could be found otherwise print “Not Possible”.
- If it is possible , then print the left string and right string.
Below is the implementation of the above approach
Time Complexity: O(N) where N is the length of given string.
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.
- Count ways to partition a string such that both parts have equal distinct characters
- Check if both halves of the string have at least one different character
- Check if both halves of the string have same set of characters
- Check if both halves of the string have same set of characters in Python
- Least number of manipulations needed to ensure two strings have identical characters
- Split a string in equal parts such that all parts are palindromes
- Partition a number into two divisible parts
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Print all positions of a given string having count of smaller characters equal on both sides
- Split the string into minimum parts such that each part is in the another string
- Count items common to both the lists but with different prices
- Find two equal subsequences of maximum length with at least one different index
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Ways to split string such that each partition starts with distinct character
- Number of sub-arrays that have at least one duplicate
- Sub-strings that start and end with one character and have at least one other
- Number of ways to partition a string into two balanced subsequences
- Maximize partitions such that no two substrings have any common character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.