TT22 icon

permutations.java

TT22 | PRO | 09/10/17 01:28:47 PM UTC | 0 ⭐ | 231 👁️ | Never ⏰ | []
Java |

1.32 KB

|

None

|

0 👍

/

0 👎

static HashSet<String> permutations(String t) {
         //Instanciation des variables
        String racine;
        HashSet<String> ch = new HashSet<String>();
        String h;
        HashSet<String> result = new HashSet<String>();
        //LinkedList<String> ret = new LinkedList<String>();
         
        int i, a;
        //Si le mot est composé d'un seul caractère
        if (t.length() == 1) {
            result.add(t);
            return result;
        } else {
            //On boucle sur toute les lettres
            for (i = 0; i < t.length(); i++) {
                //on récupére la lettre courante
                racine = t.substring(i, i+1);
                //if (ret.indexOf(racine) < 0) {
                    //ret.add(racine);
                    h=t;
                    for(int j=0;j<h.length();j++){
                        if(h.charAt(j) == t.charAt(i)) h = h.substring(0, j) + h.substring(j+1);
                    }
                    //a = result.size();
                    ch = permutations(h);
                    result.addAll(ch);
                    for (String s : ch){
                          result.add(racine + s);
                          result.remove(s);
                    }
                //}
            }
            return result;
        }         
  }

Comments