fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.println(shortenText("The automatic gain control control"));
  13. }
  14.  
  15. static Map<String, String> ABBREVIATION_MAP = new TreeMap<>(new Comparator<String>() {
  16. // Sort keys descending by length
  17. public int compare(String a, String b) {
  18. return b.length() - a.length();
  19. }
  20. });
  21.  
  22. static {
  23. ABBREVIATION_MAP.put("CONTROL", "CNTRL");
  24. ABBREVIATION_MAP.put("AUTOMATIC GAIN CONTROL", "AGC");
  25. ABBREVIATION_MAP.put("THE", "");
  26. }
  27.  
  28. static public String shortenText(String input) {
  29. StringBuilder result = new StringBuilder(input).insert(0, " ").append(" ");
  30.  
  31. for (Map.Entry<String, String> entry : ABBREVIATION_MAP.entrySet()) {
  32. String phrase = " " + entry.getKey() + " ";
  33. String replacement = " " + entry.getValue() + " ";
  34.  
  35. int i;
  36. do {
  37. i = result.toString().toUpperCase().indexOf(phrase);
  38. if (i > -1) {
  39. result.replace(i, i + phrase.length(), replacement);
  40. }
  41. } while (i > -1);
  42. }
  43.  
  44. return result.toString().trim();
  45. }
  46. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
AGC CNTRL