Solution to Facebook Hacker Cup 2012 “alphabet soup” Solved


Alfredo Spaghetti really likes soup, especially when it contains alphabet pasta. Every day he constructs a sentence from letters, places the letters into a bowl of broth and enjoys delicious alphabet soup.

Today, after constructing the sentence, Alfredo remembered that the Facebook Hacker Cup starts today! Thus, he decided to construct the phrase “HACKERCUP”. As he already added the letters to the broth, he is stuck with the letters he originally selected. Help Alfredo determine how many times he can place the word “HACKERCUP” side-by-side using the letters in his soup.

Input

The first line of the input file contains a single integer T: the number of test cases. T lines follow, each representing a single test case with a sequence of upper-case letters and spaces: the original sentence Alfredo constructed.

Output

Output T lines, one for each test case. For each case, output “Case #t: n”, where t is the test case number (starting from 1) and n is the number of times the word “HACKERCUP” can be placed side-by-side using the letters from the sentence.

Constraints

  • 1 < T ≤ 20
  • Sentences contain only the upper-case letters A-Z and the space character
  • Each sentence contains at least one letter, and contains at most 1000 characters, including spaces

Solution to facebook hacker cup Billboard brain teaser.

2012 Qualification Round Solutions

by Facebook Hacker Cup on Wednesday, January 25, 2012 at 1:49pm

Alphabet Soup

 

Alphabet Soup was the easiest problem in the qualifiers, with 5084 correct submissions. To solve it, simply iterate through the characters in the string, counting how frequently each of them occurs. Suppose count[X] is how many of character X are in the soup. Then the number of “HACKER CUP”s we can make is the minimum of count[X] for all letters other than C, and count[‘C’] / 2 since there are two Cs in “HACKER CUP”.

 

Here’s a solution in Python:

import sys

from collections import defaultdict

cases = sys.stdin.readlines()

for case in range(1, len(cases)):

letters = defaultdict(int)

for letter in cases[case]:

letters[letter] += 1

letters[‘C’] /= 2 # Account for the two ‘C’s

words = min(letters[x] for x in “HACKERCUP”)

print(“Case #{}: {}”.format(case, words))

 


Leave a Reply

Your email address will not be published. Required fields are marked *