# Facebook Hacker Cup Qualification Round 2013 Solution : Beautiful Strings

**Facebook Hacker Cup 2013: “Beautiful Strings” Solution**

**The problem statement is given below:**

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?

Input

The input file consists of a single integer

**m**followed by**m**lines.
Output

Your output should consist of, for each test case, a line containing the
string "Case #

**x**:**y**" where**x**is the case number (with 1 being the first case in the input file, 2 being the second, etc.) and**y**is the maximum beauty for that test case.
Constraints

5 ≤

2 ≤ length of

**m**≤ 502 ≤ length of

**s**≤ 500
Sample Input

`5`

`ABbCcc`

`Good luck in the Facebook Hacker Cup this year!`

`Ignore punctuation, please :)`

`Sometimes test cases are hard to make up.`

`So I just go consult Professor Dalves`

Sample Output

`Case #1: 152`

`Case #2: 754`

`Case #3: 491`

`Case #4: 729`

`Case #5: 646`

` `

`Here goes my solution for the “Beautiful Strings” problem:`

```
```

import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.LineNumberReader; import java.util.ArrayList; import java.util.Collections; /** * * @author VIK */ public class Problem1 { private static ArrayList < Integer > integers; private static File f=new File("Solution1.txt"); public static void main(String[] args) throws Exception { FileReader fr = new FileReader("beautiful_stringstxt.txt"); LineNumberReader lnr = new LineNumberReader(fr); int t = Integer.parseInt(lnr.readLine().trim()); FileWriter fw=new FileWriter(f); for (int i = 0; i < t; i++) { int sum= getMax(lnr.readLine().trim()); if ( i < t-1 ) fw.write ( "Case #" + ( i+1 ) + ": " + sum+ "\n" ); else fw.write( "Case #" + (i+1) + ": " + sum ); fw.flush(); } }

private static int getMax ( String string ) throws Exception { integers = new ArrayList < Integer > (); for ( int i = 65, j = 97; i <= 90 && j <= 123 ; i++, j++ ) { String s = string.replaceAll ( "[^" + (char) i + (char) j + "]", "" ); s.trim(); integers.add(s.length()); } int sum = 0; Collections.sort(integers); for ( int i = integers.size() - 1, j = 26; i >= 0 && j >= 1; --i, --j) { sum += integers.get(i) * j; } return sum; }

}

`The solution is quite efficient and generated output file for my input file provided by Facebook in less than one second.`

` `

`Do leave comment regarding any issue or suggestion.`

Admin : Vikash

What's the solution for the balanced smiley problem?

ReplyDeleteHere goes the solution : http://vikash-thiswillgoaway.blogspot.in/2013/01/facebook-hacker-cup-2013-balanced.html

DeleteThanx

Delete