Last active
December 11, 2015 21:18
-
-
Save Bekt/4661660 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| import java.io.*; | |
| //2013 Facebook Hacker Cup Qualification Round | |
| //Problem 1: Beautiful strings https://www.facebook.com/hackercup/problems.php?pid=475986555798659&round=185564241586420 | |
| public class Beautiful { | |
| static Scanner in; | |
| public static void main(String[] args) throws Exception { | |
| in = new Scanner(new File("Beautiful.in")); | |
| new Beautiful().run(); | |
| } | |
| void run() { | |
| int m = in.nextInt(), counter = 0; | |
| in.nextLine(); | |
| for (int i=0; i<m; i++) | |
| System.out.printf("Case #%d: %d%n", ++counter, solve(in.nextLine().toLowerCase())); | |
| } | |
| int solve(String line) { | |
| int[] count = new int[26]; | |
| for (int i=0; i<line.length(); i++) { | |
| char c = line.charAt(i); | |
| if (Character.isLetter(c)) | |
| count[c - 'a']++; | |
| } | |
| Arrays.sort(count); | |
| int num = 26, sum = 0; | |
| for (int i=count.length-1; i >= 0; i--) { | |
| if (count[i] == 0) break; | |
| sum += (num-- * count[i]); | |
| } | |
| return sum; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment