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.
- 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
- Java program to read all Emails present in a Given file
- Count the Number of matching characters in a pair of strings
- Java program to print Even length words in a String
- Program to accept Strings starting with a Vowel
- Program to accept String starting with Capital letter
- Check if a Number is Odd or Even using Bitwise Operators
- Print the most occurring character in an array of strings
- Program to find all Factors of a Number using recursion
- Program to accept a Strings which contains all the Vowels
- Check if count of Alphabets and count of Numbers are equal in the given String
- Smallest odd number with even sum of digits from the given number N