4.2 All permutations with duplicate elements

Solution: Use a set to record all distinct elements.

Set<Character> used = new HashSet<>();
for (int i = index; i < array.length; i++) {
  if (used.add(array[i])) {	//returns true if not exist in the set
    //swap-swap
  }
}

Last updated

Was this helpful?