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 email@example.com. 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
- K length words that can be formed from given characters without repetition
- Convert to a string that is repetition of a substring of k length
- Maximum String Partition
- Convert an unbalanced bracket sequence to a balanced sequence
- Minimum operations required to convert a binary string to all 0s or all 1s
- String Range Queries to find the number of subsets equal to a given String
- Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
- Swap characters in a String
- Check if the first and last digit of number N is prime and their sum is less than K
- Sort an array of strings according to string lengths using Map
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Round the given number to nearest multiple of 10 | Set-2
- Java program to check whether a string is a Palindrome