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. // your code goes here
  13. String[] car = {"a,b"};
  14. friendlyWords(car);
  15.  
  16. }
  17.  
  18. private static int getHash(int[] count) {
  19. int hash = 0;
  20. int a = 378551;
  21. int b = 63689;
  22. for (int num : count) {
  23. hash = hash * a + num;
  24. a = a * b;
  25. }
  26. return hash;
  27. }
  28.  
  29. static String[] friendlyWords(String[] input) {
  30. if(input == null)
  31. return new String[]{};
  32. ArrayList<String> result = new ArrayList<String>();
  33. HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
  34.  
  35. for (String str : input) {
  36. str.toLowerCase();
  37. str.trim();
  38. int[] count = new int[26];
  39. for (int i = 0; i < str.length(); i++) {
  40. count[str.charAt(i) - 'a']++;
  41. }
  42.  
  43. int hash = getHash(count);
  44. if (!map.containsKey(hash)) {
  45. map.put(hash, new ArrayList<String>());
  46. }
  47.  
  48. map.get(hash).add(str);
  49. }
  50.  
  51. for (ArrayList<String> tmp : map.values()) {
  52.  
  53. if (tmp.size() > 1) {
  54. Collections.sort(tmp);
  55. String line_temp = "";
  56. for(int i=0;i<tmp.size();i++) {
  57. line_temp = line_temp + tmp.get(i) + " ";
  58. }
  59. line_temp.trim();
  60. result.add(line_temp);
  61. }
  62. }
  63. String[] output_final = new String[result.size()];
  64. output_final= result.toArray(output_final);
  65. Arrays.sort(output_final);
  66. if(output_final != null)
  67. return output_final;
  68. else
  69. return new String[]{};
  70. }
  71. }
Runtime error #stdin #stdout #stderr 0.04s 4386816KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -53
	at Ideone.friendlyWords(Main.java:40)
	at Ideone.main(Main.java:14)