Distributing all balls without repetition - GeeksforGeeks

29 Apr, 2022
Given N balls. For convenience, we denote the color of each ball as — lowercase letters. 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 balls, but, we have to distribute all the balls, such that no one will be upset — print YES, if it is possible, and NO, otherwise.
Examples: 
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.  
C++
Java
Python3
C#
Javascript
// CPP program to find if its possible to// distribute balls without repetition#include <bits/stdc++.h>using namespace std; const int MAX_CHAR = 26; // function to find if its possible to// distribute balls or notbool distributingBalls(int k, int n, string str){      // count array to count how many times    // each color has occurred    int a[MAX_CHAR] = {0};    for (int i = 0; i < n; i++){                 // increasing count of each color        // every time it appears        a[str[i] - 'a']++;     }         for (int i = 0; i < MAX_CHAR; i++)           // to check if any color appears        // more than K times if it does        // we will print NO        if (a[i] > k)          return false;     return true;} // Driver codeint main(){    long long int n = 6, k = 3;    string str = "aacaab";     if (distributingBalls(k, n, str))        cout << "YES";    else        cout << "NO";    return 0;}
// Java program to find if its possible// to distribute balls without repetitionimport java.io.*; public class GFG {     static int MAX_CHAR = 26;         // function to find if its possible    // to distribute balls or not    static boolean distributingBalls(long k,                         long n, String str)    {                 // count array to count how many        // times each color has occurred        int []a = new int[MAX_CHAR];                 for (int i = 0; i < n; i++)        {                         // increasing count of each            // color every time it appears            a[str.charAt(i) - 'a']++;        }                 for (int i = 0; i < MAX_CHAR; i++)                 // to check if any color appears            // more than K times if it does            // we will print NO            if (a[i] > k)                return false;             return true;    }         // Driver code    static public void main (String[] args)    {        long n = 6, k = 3;        String str = "aacaab";             if (distributingBalls(k, n, str))            System.out.println("YES");        else            System.out.println("NO");    }} // This code is contributed by vt_m.
# Python3 program to find if its possible to# distribute balls without repetition  MAX_CHAR = 26 # function to find if its possible to# distribute balls or notdef distributingBalls(k, n, string) :     # count array to count how many times    # each color has occurred    a = [0] * MAX_CHAR         for i in range(n) :        # increasing count of each color        # every time it appears        a[ord(string[i]) - ord('a')] += 1         for i in range(MAX_CHAR) :        # to check if any color appears        # more than K times if it does        # we will print NO        if (a[i] > k) :            return False         return True  # Driver codeif __name__ == "__main__" :      n, k = 6, 3    string = "aacaab"     if (distributingBalls(k, n, string)) :        print("YES")    else :        print("NO") # This code is contributed by Ryuga
// C# program to find if its possible to// distribute balls without repetitionusing System; public class GFG {     static int MAX_CHAR = 26;         // function to find if its possible    // to distribute balls or not    static bool distributingBalls(long k,                       long n, string str)    {                 // count array to count how many        // times each color has occurred        int []a = new int[MAX_CHAR];                 for (int i = 0; i < n; i++)        {                         // increasing count of each            // color every time it appears            a[str[i] - 'a']++;        }                 for (int i = 0; i < MAX_CHAR; i++)                 // to check if any color            // appears more than K            // times if it does we            // will print NO            if (a[i] > k)                return false;             return true;    }         // Driver code    static public void Main ()    {        long n = 6, k = 3;        string str = "aacaab";             if (distributingBalls(k, n, str))            Console.WriteLine("YES");        else            Console.WriteLine("NO");    }} // This code is contributed by vt_m.
<script>    // Javascript program to find if its possible to    // distribute balls without repetition         let MAX_CHAR = 26;           // function to find if its possible    // to distribute balls or not    function distributingBalls(k, n, str)    {                   // count array to count how many        // times each color has occurred        let a = new Array(MAX_CHAR);        a.fill(0);                   for (let i = 0; i < n; i++)        {                           // increasing count of each            // color every time it appears            a[str[i].charCodeAt() - 'a'.charCodeAt()]++;        }                   for (let i = 0; i < MAX_CHAR; i++)                   // to check if any color            // appears more than K            // times if it does we            // will print NO            if (a[i] > k)                return false;               return true;    }         let n = 6, k = 3;    let str = "aacaab";     if (distributingBalls(k, n, str))      document.write("YES");    else      document.write("NO");     </script>
Output:  
NO
This article is contributed by Sarthak Kohli. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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. 
vt_m
ankthon
suresh07
surinderdawra388
School Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Constructors in C++
Operator Overloading in C++
Copy Constructor in C++
Polymorphism in C++
Write a program to reverse an array or string
Write a program to print all permutations of a given string
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching