TT22

permutations.java

Sep 10th, 2017
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. static HashSet<String> permutations(String t) {
  2.          //Instanciation des variables
  3.         String racine;
  4.         HashSet<String> ch = new HashSet<String>();
  5.         String h;
  6.         HashSet<String> result = new HashSet<String>();
  7.         //LinkedList<String> ret = new LinkedList<String>();
  8.          
  9.         int i, a;
  10.         //Si le mot est composé d'un seul caractère
  11.         if (t.length() == 1) {
  12.             result.add(t);
  13.             return result;
  14.         } else {
  15.             //On boucle sur toute les lettres
  16.             for (i = 0; i < t.length(); i++) {
  17.                 //on récupére la lettre courante
  18.                 racine = t.substring(i, i+1);
  19.                 //if (ret.indexOf(racine) < 0) {
  20.                     //ret.add(racine);
  21.                     h=t;
  22.                     for(int j=0;j<h.length();j++){
  23.                         if(h.charAt(j) == t.charAt(i)) h = h.substring(0, j) + h.substring(j+1);
  24.                     }
  25.                     //a = result.size();
  26.                     ch = permutations(h);
  27.                     result.addAll(ch);
  28.                     for (String s : ch){
  29.                           result.add(racine + s);
  30.                           result.remove(s);
  31.                     }
  32.                 //}
  33.             }
  34.             return result;
  35.         }        
  36.   }
Advertisement
Add Comment
Please, Sign In to add comment