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
- Count of matchsticks required to represent the given number
- Program to implement FLAMES game
- Program to print the Ladder Pattern
- Program to print the Diagonals of a Matrix
- C Program to concatenate two strings without using strcat
- Print characters in decreasing order of frequency
- Minimum number of additons to make the string balanced
- Extract all integers from the given string in Java
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- C program to print the length of a String using %n format specifier