isefire

Facebook Hacker Challenge 2013

Sep 13th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. import java.io.File;
  2. import java.util.Scanner;
  3. import java.util.Arrays;
  4. public class BeautifulStrings
  5. {
  6.     public static int totalBeauty(String line)
  7.     {
  8.         //build alphabet/freq/val triplets
  9.         String[][] alphabet_freq = { {"a", "0"},
  10.                                     {"b", "0"},
  11.                                     {"c", "0"},
  12.                                     {"d", "0"},
  13.                                     {"e", "0"},
  14.                                     {"f", "0"},
  15.                                     {"g", "0"},
  16.                                     {"h", "0"},
  17.                                     {"i", "0"},
  18.                                     {"j", "0"},
  19.                                     {"k", "0"},
  20.                                     {"l", "0"},
  21.                                     {"m", "0"},
  22.                                     {"n", "0"},
  23.                                     {"o", "0"},
  24.                                     {"p", "0"},
  25.                                     {"q", "0"},
  26.                                     {"r", "0"},
  27.                                     {"s", "0"},
  28.                                     {"t", "0"},
  29.                                     {"u", "0"},
  30.                                     {"v", "0"},
  31.                                     {"w", "0"},
  32.                                     {"x", "0"},
  33.                                     {"y", "0"},
  34.                                     {"z", "0"} };
  35.         line = line.toLowerCase();
  36.         StringBuffer buff = new StringBuffer(line);
  37.         final int BASE_ALPHA = 97;
  38.         //97 - 122 (int) char
  39.         for (int i = 0; i < buff.length(); i++)
  40.         {
  41.             char temp = buff.charAt(i);
  42.             int tempnum = (int) temp;
  43.             int setfreq = tempnum-BASE_ALPHA;
  44.             if (tempnum >= 97 && tempnum <= 122)
  45.             {
  46.                 for (int j = i; j < buff.length(); j++)
  47.                 {
  48.                     char tempother = buff.charAt(j);
  49.                     if (tempother == temp)
  50.                     {
  51.                         buff.replace(j,j+1," ");
  52.                         //add to freq array
  53.                         int tempArrayValue = Integer.parseInt(alphabet_freq[setfreq][1]);
  54.                         tempArrayValue += 1;
  55.                         alphabet_freq[setfreq][1] = Integer.toString(tempArrayValue);
  56.                     }
  57.                 }
  58.             }
  59.             else
  60.             {
  61.                 continue;
  62.             }
  63.         }
  64.         //initialize from 26-1 if whole alphabet is represented
  65.         boolean end = false;
  66.         int k = 0;
  67.         int total = 0;
  68.         int first = 26;
  69.         int currentLargestPlace = 0;
  70.         int currentLargestFreq = 0;
  71.         while (end == false)
  72.         {
  73.             //debug
  74.             //System.out.println(Arrays.toString(alphabet_freq[k]));
  75.             int thisFreq = Integer.parseInt(alphabet_freq[k][1]);
  76.             if (thisFreq > currentLargestFreq)
  77.             {
  78.                 //System.out.println("First if block");
  79.                 currentLargestFreq = thisFreq;
  80.                 currentLargestPlace = k;
  81.             }
  82.             //when reach end, first check if 0
  83.             if (k == 25)
  84.             {
  85.                 //System.out.println("Second if block");
  86.                 if (currentLargestFreq == 0)
  87.                 {
  88.                     //System.out.println("Embedded if block");
  89.                     end = true;
  90.                     break;
  91.                 }
  92.                 total += currentLargestFreq * first;
  93.                 //System.out.println("Current total = " + total);
  94.                 first -= 1;
  95.                 currentLargestFreq = 0;
  96.                 alphabet_freq[currentLargestPlace][1] = "0";
  97.                 k = 0;
  98.             }
  99.             else
  100.             {
  101.                 k++;
  102.             }
  103.         }
  104.         return total;
  105.     }
  106.     public static void main(String[] args)
  107.     {
  108.         try
  109.         {
  110.             File file = new File(args[0]);
  111.             Scanner scanner = new Scanner(file);
  112.             while(scanner.hasNextLine())
  113.             {
  114.                 System.out.println(totalBeauty(scanner.nextLine()));
  115.             }
  116.         }
  117.         catch (Exception e)
  118.         {
  119.             System.out.println(e);
  120.         }
  121.     }
  122. }
Add Comment
Please, Sign In to add comment