This weekend I participated at the anual coding contest organised by Facebook. It is the second edition and there are still enough things to improve. Although I did well on programming I didn’t qualified for the next round because of a stupid mistake, which I’ve done it twice.
The last problem (“Alphabet soup”) was the easiest and it enounces like that:

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
Example input
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

Example output

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

I solved it in PHP, but I tested with the data from the page pasted into a file on my computer. In less than a half an hour you can figure it out , program and test it, but I hadn’t a good setup for reading the file and when I runned the file downloaded from facebook, it gave me an extra 21 line when the number of test cases was mentioned as 20. So, because I tested few days before a problem from last year which had a separation between the number of test cases and the data, I hastly modified the program to remove a line after the number of cases.

In short, I had a the correct result without the first line and numbered whith 1 in minus. Here is my solution:

<?php
function fromTxtToArray($filename){
    $fh = fopen($filename, "rb");
    $data = fread($fh, filesize($filename));
    fclose($fh);

    $array = explode("\n",$data);
    $rows = $array[0];
    unset($array[0]);
    echo $rows; // I realised afterwards that $rows != count($data);
    return array('rows' => $rows, 'data' => $array);
}

function Alphabet($input){
    $target = Array();
    $cazul = str_split($input);
    $map = array_count_values($cazul); //the builtin function does all the job
    //print_r($map); each letter whith the number of occurences
    $target['H'] = isset($map['H'])?$map['H']:0;
    $target['A'] = isset($map['A'])?$map['A']:0;    
    $target['C'] = isset($map['C'])?(floor($map['C']/2) ):0; //there are 2 C in the word
    $target['K'] = isset($map['K'])?$map['K']:0;
    $target['E'] = isset($map['E'])?$map['E']:0;
    $target['R'] = isset($map['R'])?$map['R']:0;
    $target['U'] = isset($map['U'])?$map['U']:0;
    $target['P'] = isset($map['P'])?$map['P']:0;    
    $low = min($target);

    return $low;    
}

$data = fromTxtToArray('alphabet_soup.txt');

$i = 1;
$output = '';

foreach ($data['data'] as $k => $v){
    $output .= 'Case #' . $i . ': ' . Alphabet(trim($v))."\r\n";
    $i++;
    if($i > $data['rows']) break; //the stupid condition which I omitted
}
$fp = fopen('output_soup.txt', 'w');
fwrite($fp, $output);
fclose($fp);
echo $output;

The problems are quite original and except of this they are not trivial to solve. There are a lot of very good programmers and I didn’t have high hopes to get to the finals. But I’m sory that I’ve solved two problems and I failed for such a stupid mistake.

UPDATE :The other problem I solved it wrong, although it worked on the test dataset.