Generate all possible permutations from a string with character repetitions I want to know if my code is good, with reasonable efficiency, and if it is possible to improve my code and how. What would you suggest?

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class FindAllPermutationsWithRepetitions {

    public static void main(String[] args) {
        String str = "ferarri";
        Set&lt;String&gt; permutations = getPermutations(str);

        int i = 1;
        for(String s : permutations){
            System.out.println(i++ + ": " + s);
        }

        /* check correctness */
        System.out.println("\nThere must be " +  numOfArrangements(str) + " values");

    }

    public static Set&lt;String&gt; getPermutations(String remained){
        if(remained.length() &lt;= 1){
            Set&lt;String&gt; set = new HashSet&lt;String&gt;();
            set.add(remained);
            return set;
        }

        char c = remained.charAt(0);
        Set&lt;String&gt; old_permutations = getPermutations(remained.substring(1));
        Set&lt;String&gt; new_permutations = new HashSet&lt;String&gt;();

        for(String old_s : old_permutations){
            for(int j = 0; j &lt; old_s.length(); j++){
                new_permutations.add(old_s.substring(0, j) + c + old_s.substring(j));
            }
            new_permutations.add(old_s + c);
        }

        return new_permutations;
    }

    public static int factorial(int num){
        int result = 1;

        for (int i = 1; i &lt;= num; i++) {
            result = result * i;
        }

        return result;
    }

    public static int numOfArrangements(String str){
        HashMap&lt;Character, Integer&gt; hm = new HashMap();

        for(char key : str.toCharArray()){
            if(hm.containsKey(key)) hm.put(key, hm.get(key) + 1);
            else hm.put(key, 1);
        }

        int divisor = 1;
        for(Integer value : hm.values()){
            divisor *= factorial(value);
        }

        return factorial(str.length()) / divisor;
    }
}
My solution below uses a custom radix counter and then maps the value to each character in a given String. I have used BigInteger as you will hit problems with ints once your String gets to about 10 characters long, and the same problem with longs once your String gets to around 16 characters.

I have wrapped it all in one class so you can see it in action, but given the Counter class is completely independent, you should have that in it's own file.

package permutationgenerator;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Iterator;

public class StringPermutationGenerator implements Iterable&lt;String&gt; {
    private final String word;

    public StringPermutationGenerator(String word) {
        this.word = word;
    }

    @Override
    public Iterator&lt;String&gt; iterator() {
        return new StringIterator(word);
    }

    private class StringIterator implements Iterator&lt;String&gt; {
        private final char[] alphabet;
        private final Counter counter;
        private final BigInteger max;
        private BigInteger count;

        private StringIterator(String word) {
            alphabet = word.toCharArray();
            counter = new Counter(alphabet.length, alphabet.length);
            max = BigInteger.valueOf(alphabet.length).pow(alphabet.length);
            count = BigInteger.ZERO;
        }

        @Override
        public boolean hasNext() {
            return count.compareTo(max) &lt; 0;
        }

        @Override
        public String next() {
            StringBuilder sb = new StringBuilder();
            for (int i : counter.getCounter()) {
                sb.append(alphabet[i]);
            }
            counter.increment();
            count = count.add(BigInteger.ONE);
            return sb.toString();
        }

    }

    private class Counter {
        private final int radix;
        private final int length;
        private final int[] counter;

        public Counter(int radix, int length) {
            this.radix = radix;
            this.length = length;
            counter = new int[length];
        }

        public int[] getCounter() {
            return counter;
        }

        public void increment() {
            for (int index = length - 1; ; index--) {
                if (index == -1) {
                    resetCounter();
                    return;
                }
                if (counter[index] &lt; radix - 1) {
                    counter[index]++;
                    return;
                }
                counter[index] = 0;
            }
        }

        private void resetCounter() {
            Arrays.fill(counter, 0);
        }
    }
}


Usage is as such:

for (String s : new StringPermutationGenerator("ferrari")) {
    System.out.println(s);
}