Given N balls. For convenience, we denote color of each ball as — lowercase letter. We have to distribute N balls among K people. They will be upset, if they get two balls of the same color. We can give any number of balls to people and they won’t be upset even if they do not get any ball, but, we have to distribute all balls, such that no one will be upset — print YES, if it is possible , and NO, otherwise.
Input : 4 2 // value of N and K aabb // colors of given balls Output : YES We can give 1st and 3rd ball to the first person, and 2nd and 4th to the second. Input : 6 3 // value of N and K aacaab // colors of given balls Output : NO We need to give all balls of color a, but one ball will stay, that's why answer is NO
The approach will be really simple, we will create a count array to keep the count of each color that occurs and then we will check if any color occurs more than the number of people we have. If it occurs we will print NO else YES.
The implementation of the above idea is given below.
This article is contributed by Sarthak Kohli. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print all permutations with repetition of characters
- Convert to a string that is repetition of a substring of k length
- K length words that can be formed from given characters without repetition
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Longest substring that starts with X and ends with Y
- Print all possible palindromic string formed using any pair of given strings
- Find indices of all local maxima and local minima in an Array
- Minimum steps to convert X to Y by repeated division and multiplication
- Check if a string can be split into two strings with same number of K-frequent characters
- Count of substrings having all distinct characters
- Calculate weight of parenthesis based on the given conditions
- Smallest number greater than X which is K-periodic
- Check if a given string is a Reverse Bitonic String or not
- Check if a given string is Even-Odd Palindrome or not