Facebook Hacker Cup - Beautiful Strings
Now that the Facebook Hacker Cup is coming to an end I figured I’d post my solution to one of the challenges. Unfortunately I only made it to Round 1, but I was able to answer this rather interesting Qualification Round problem.
When John was a little kid he didn’t have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could… he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.
Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.
The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn’t care about whether letters are uppercase or lowercase, so that doesn’t affect the beauty of a letter. (Uppercase ‘F’ is exactly as beautiful as lowercase ‘f’, for example.)
You’re a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?
The input file consists of a single integer m, followed by m lines.
You can find the input file here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
To start, I read the input.txt (as provided by Facebook) into R, saving each line of input as a separate character string in the ‘inputs’ vector. I then saved the first entry of ‘inputs’ as m in accordance with Facebook’s description of the problem and then subsetted the whole vector to only include the actual strings we are to analyze.
After allocating some memory for an outputs vector and using R’s built in letters variable (it might have been redundant to name it as a variable) I began a for loop to actually calculate the maximum beauty of each string. Inside the for loop, I created local variables x and y. x was simply the ith input in all lowercase (for homogeneity). I then created y to split the strings into vectors which contained one character per entry and removed all non-letter components through subsetting.
From there, I created a table to look at the number of times each letter appeared in the i’th string and sorted it by decreasing. A nested for loop assigned values to the elements in the table that I had constructed. I assigned 26 to the letter that appeared most frequently, 25 to the second most frequent, and so on. In this way we calculate the MAXIMUM POSSIBLE beauty of each string.
Finally, I filled the outputs vector by creating a string in correspondence with Facebook’s requirements. Once we rinse and repeat this process a total of m times, we have a complete outputs vector that stores the maximum beauty of each string in inputs.txt.
In order to produce the outputs as a .txt file to upload to Facebook, I employed a simple
writeLines function which placed every element of the outputs vector on a separate line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21