FbHc12 - Soup Problem Solution

Nishant Arora 21/Jan/2012
Facebook
Twitter
LinkedIn
Reddit

Update: Since the submissions are over, you can check my source code here (online IDE)

Plain 15 minutes is what it took to design the fastest solution, the problem statement is as follows:

Alphabet Soup

Problem Statement

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

Sample Input File:

5
WELCOME TO FACEBOOK HACKERCUP
CUP WITH LABEL HACKERCUP BELONGS TO HACKER
QUICK CUTE BROWN FOX JUMPS OVER THE LAZY DOG
MOVE FAST BE BOLD
HACK THE HACKERCUP

Sample Output File

Case #1: 1
Case #2: 2
Case #3: 1
Case #4: 0
Case #5: 1

The Solution:

You just need to confirm that in each input string there are characters H,A,C,K,E,R,U,P and here the "C" will come twice, make those as the sub sets, so the number of existing subsets will be the answer.

Algorithm:

  1. Remove all the spaces from the input string.
  2. Convert all characters into a datatype, I prefer arrays
  3. Count the values of required data. (Hint: I use the array_count_values(); in PHP)
  4. If the required number is missing out put zero
  5. Else the char having the lowest count value will be the answer (Remember "C" has to be counted twice).

Source Code: 

Since the submissions are over, you can check my source code here (online IDE)

Working Example:

Click here to access the working solution of the problem, to check your answers.

Cheers!...

Hope you guys like it, I am open to Comments, Suggestions and Discussions.