facebook Hacker Cup 2012 Qualification Round - alphabet soup
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;i<input.length;i++){
- var char = input[i];
- if(!map[char]){
- map[char] = 1;
- }else{
- map[char] = map[char]+1;
- }
- }
- //Check check HackerCup
- var testStr = "HACKERCUP";
- var wordCount = 0;
- while(true){
- for(var i=0;i<testStr.length;i++){
- var char = testStr[i];
- if(map[char]>0){
- 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 <= t; c++) {
- String s = in.nextLine();
- int[] ct = new int[26];
- for (int i = 0; i < s.length(); i++) {
- if (s.charAt(i)==' ') continue;
- int cc = s.charAt(i)-'A';
- ct[cc]++;
- }
- int min = 10000;
- min = Math.min(min, ct['H'-'A']);
- min = Math.min(min, ct['A'-'A']);
- min = Math.min(min, ct['C'-'A']/2);
- min = Math.min(min, ct['K'-'A']);
- min = Math.min(min, ct['E'-'A']);
- min = Math.min(min, ct['R'-'A']);
- min = Math.min(min, ct['U'-'A']);
- min = Math.min(min, ct['P'-'A']);
- System.out.printf("Case #%d: %d%n", c, min);
- }
- }
- }
Next part will be my winning entry for the billboard entry.
- Login to post comments
Comments
Anonymous (not verified) - Wed, 01/25/2012 - 17:27
You wrote a blog post on counting the number of letters in a word?
Christian (not verified) - Thu, 01/26/2012 - 07:17
It is a blogpost about counting random sorted word-letters in a string - because this was the problem asked in the facebook hackercup.