This is in Java
==
public class Permute {
public static void main(String[] args) {
permute("", "ABCD");
}
public static void permute(String parent, String s) {
if (s.length() == 2) {
System.out.println(parent + s);
Yet another approach. If instance size is n,
A B C D a b c d
1. swap the element at n/2 and n-1
A B C c a b D d
2. shift "a" and "b"
A B a b C c D d
Now the instance size is n/2 (A B a b) Recursively perform steps 1. and
2. on it.
The swaps are constant only the shifts are a function of n.