This year qualification round had three problem. The easy one was Alphabet Soup – classic character counting problem.
problem description
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.
solution in javascript
First you have to count all digits from the input line. For the result you have to get the lowest character count for the word HACKERCUP – but keep in mind that C is twice in the string.
function task(input){ var map = {}; //Count chars for(var i=0;i0){ map[char] = map[char]-1; }else{ return wordCount; } } wordCount++; } return input; }
This solution (here only the solve-function) from tobias made the qualification point! I built an improved solution with more javascript-like usage of the object. For shorter and improved code I used the no-go
with
statement to get the
Math.min( H,A,C,K,E,R,C,U,P )
-result. You can test the code live on codejamer.com.
input data | result data |
---|---|
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 |
Case #1: 1 Case #2: 2 Case #3: 1 Case #4: 0 Case #5: 1 |
({ //using the codejamer.com framework init: function(input) { input.shift(); return input; }, task: function(input, return) { var i, d = {}, h = "HACKERCUP"; //init the digit counter for (i=0; i < h.length; i++){ d[h[i]]=0; } //count all characters for (i=0; i < input.data.length; i++){ d[input.data[i]] += (input.data[i] in d); } d.C = Math.floor(d.C/2); //get the minimal word count for HACKERCUP with (d) return ('Case #' + input.index + ': ' + Math.min( H,A,C,K,E,R,C,U,P )); } })
top score solution in java
The top score coder had used java or c++ for solving the problem. The code in the TOP10 is larger than the short javascript solution but works the same way. On place 9 I found a readable java version with a simple array for the 26 defined characters.
import java.util.Scanner; public class AlphabetSoup { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); in.nextLine(); for (int c = 1; c
Next part will be my winning entry for the billboard entry.

Christian Harms

Latest posts by Christian Harms (see all)
- google code jam 2013 – tic-tac-toe-Tomek solution - April 16, 2013
- Google code jam 2013 – the lawnmower - April 14, 2013
- code puzzles and permutations - April 11, 2013