Facebook Hacker Cup 2013:Beautiful Strings solution in PHP


Here is the solution for FHC 2013 Beautiful Strings problem in PHP:

$ip = fopen(‘input.txt‘, “r”);
$test_cases = trim(fgets($ip));
    $c=1;
    while($c!= $test_cases+1){
        $case=preg_replace(‘/[^a-z]+/’, ”, strtolower(trim(fgets($ip))));
        $vals=array_count_values(str_split($case));
        arsort($vals);
        $beauty=26;
        $max_b=0;
        foreach($vals as $v){
            $max_b +=$v*$beauty;
            $beauty–;
        }
        print(“Case #”.$c.”: “.$max_b);
        if($c < $test_cases){
            print(“
”);
        }
        $c++;
    }

Replace the red highlighted text with the input file name.
Sample Output

Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729
Case #5: 646