fork download
  1. import java.util.*;
  2.  
  3. class App {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String a[] = {"ola","lao","aol","asd","asdf","fdsa"};
  8. List<Integer> lista = new ArrayList<Integer>(Collections.nCopies(a.length, 0));
  9.  
  10. int num=1;
  11. for(int i=0; i<a.length; i++){
  12. if(lista.get(i)==0){
  13. lista.set(i, num);
  14. for(int j=i+1; j<a.length; j++)
  15. if( lista.get(j)==0 && sameChars(a[i], a[j]))
  16. lista.set(j, num);
  17. num++;
  18. }
  19. }
  20.  
  21. num--;
  22. StringBuilder sb[] = new StringBuilder[num];
  23. for(int i=0; i<num; i++)
  24. sb[i] = new StringBuilder();
  25.  
  26.  
  27. for(int i=0; i<lista.size(); i++){
  28. sb[lista.get(i)-1].append(a[i]);
  29. sb[lista.get(i)-1].append(" ");
  30. }
  31.  
  32. String[] str = new String[sb.length];
  33. int i=0;
  34. for(StringBuilder s : sb)
  35. str[i++] = s.deleteCharAt(s.length()-1).toString();
  36.  
  37. imprime(str);
  38. }
  39.  
  40.  
  41. static boolean sameChars( String firstStr , String secondStr ) {
  42. char[ ] first = firstStr.toCharArray( );
  43. char[ ] second = secondStr.toCharArray( );
  44. Arrays.sort( first );
  45. Arrays.sort( second );
  46. return Arrays.equals( first , second );
  47. }
  48.  
  49. static void imprime(String[] sb) {
  50. int num = sb.length;
  51. System.out.print("{");
  52. for(int i=0; i < num; i++){
  53. System.out.print("\""+sb[i]+"\"");
  54. if(i!=num-1)
  55. System.out.print(", ");
  56. }
  57. System.out.print("}");
  58. }
  59. }
Success #stdin #stdout 0.09s 320512KB
stdin
Standard input is empty
stdout
{"ola lao aol", "asd", "asdf fdsa"}