solution


  • 0
    P

    Problem Statement

    Jane's two little cousins are visiting her for the holidays. As you may know, children love candy, so Jane has bought several boxes of sweets for them. Each box contained an assorted collection of candy of different types.

    At some point Jane realized that giving each child some of the boxes is not a good idea: if they receive different candies, it will certainly cause a quarrel. Therefore, Jane decided to open all the boxes, take out all the sweets and distribute them evenly into two plastic bags.

    Of course, distributing a pile of assorted candies evenly may not always be possible. Sometimes it happens that the number of pieces of some particular type of candy is odd. In that case Jane will eat one of those sweets and only then will she distribute the rest of them evenly into the bags.

    You are given the String[] boxes. Each element of boxes describes the contents of one of the boxes Jane bought. Each character in each element of boxes represents one piece of candy. Equal characters are candies of the same type: e.g., all 'B's are identical bonbons and all 'L's are identical lollipops.

    Compute and return the number of candies each child will receive.

    Definition

    Class: JanesCandyBags
    Method: getSize
    Parameters: String[]
    Returns: int
    Method signature: int getSize(String[] packets)
    (be sure your method is public)

    Constraints

    • boxes will contain between 1 and 50 elements, inclusive.
    • Each element of boxes will contain between 1 and 50 characters, inclusive.
    • Each character in boxes will be an uppercase letter of the English alphabet ('A'-'Z').

    Examples / Test cases

    {"FOO", "BAR", "BAZ", "TOPCODER"}

    Returns: 5

    In this case Jane will eat the 7 candies that only occur once in the entire collection. The remaining 10 candies can be split evenly: each bag will contain two 'O's, one 'A', one 'B', and one 'R'. Thus, each child will receive 5 candies.

    {"X"}

    Returns: 0

    Jane will eat the only piece of candy. Then she will give each child an empty bag.

    {"GIVING", "CANDY", "TO", "CHILDREN", "CANNOT", "BE", "CHALLENGING", "RIGHT"}

    Returns: 17

    The candies in each bag here are: ACCDEGGHIILNNNORT

    {"WITHOUT", "IT", "IM", "JUST", "ESPR"}

    Returns: 5

    {"PHQGHUMEAYLNLFDXFIRCVSCXGGBWKFNQDUXWFNFOZVSRTKJPRE",
    "PGGXRPNRVYSTMWCYSYYCQPEVIKEFFMZNIMKKASVWSRENZKYCXF",
    "XTLSGYPSFADPOOEFXZBCOEJUVPVABOYGPOEYLFPBNPLJVRVIPY",
    "AMYEHWQNQRQPMXUJJLOOVAOWUXWHMSNCBXCOKSFZKVATXDKNLY",
    "JYHFIXJSWNKKUFNUXXZRZBMNMGQOOKETLYHNKOAUGZQRCDDIUT",
    "EIOJWAYYZPVSCMPSAJLFVGUBFAAOVLZYLNTRKDCPWSRTESJWHD",
    "IZCOBZCNFWLQIJTVDWVXHRCBLDVGYLWGBUSBMBORXTLHCSMPXO",
    "HGMGNKEUFDXOTOGBGXPEYANFETCUKEPZSHKLJUGGGEKJDQZJEN",
    "PEVQGXIEPJSRDZJAZUJL"}

    Returns: 203


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.