public class CombinationPermutation { public String str = "abc"; public int i = 0; //work as binary array public static void main(String[] args) { CombinationPermutation cp = new CombinationPermutation(); cp.combinations(); cp.permutations(); } /** * Loop through numbers from 1 to 2^length * For each number if bit is set, append them */ private void combinations() { int n = str.length(); for (int i = 1; i < (1 << n); i++) { String s = ""; for (int j = 0; j < n; j++) { if ((i & 1<<j) != 0) { // append chars for set bit s += str.charAt(j); } } System.out.println(s); } } /** * @param res * current permutation string formed * @param set * current set of characters from which permutation needs to be formed */ private void permutations(String res,String set) { if(res.length()==set.length()) System.out.println(res); for(int j=0;j<set.length();j++) { if((i&1<<j) == 0) { i|=1<<j; //set available free 0 bit permutations(res+set.charAt(j),set); i&=~(1<< j); //unset that bit to 0 } } } /** * For each combination found , find all its permutation * same code as combination */ private void permutations() { int n = str.length(); for (int i = 1; i < (1 << n); i++) { String s = ""; for (int j = 0; j < n; j++) { if ((i & 1<<j) != 0) { // append chars for set bit s += str.charAt(j); } } permutations("",s); } } }
Tuesday, July 5, 2011
Finding all combinations/permutations using bit operator
This is a simple problem of finding all combination/permutation of a given set of characters/words/numbers. What I want to illustrate is the beauty of bit operators. Usually, we tend to skip bit operations in code. Following code illustrates it :
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment