import java.util.ArrayList; class sigmaStar { int length = 0; ArrayList<ArrayList<Integer>> setForLength; int iteration = 0; void powerSet() { setForLength = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> originalArray = new ArrayList<>(); setForLength.add(new ArrayList<Integer>()); for(int i = 0; i < length; i++) { originalArray.add(i); } powerSetHelper(new ArrayList<Integer>(), originalArray, 0); } void powerSetHelper(ArrayList<Integer> array, ArrayList<Integer> originalArray, int index) { int i = index; while(i < originalArray.size()) { ArrayList<Integer> temp = copy(array); temp.add(originalArray.get(i)); setForLength.add(temp); i++; powerSetHelper(temp, originalArray, i); } } //to make a deep copy ArrayList<Integer> copy(ArrayList<Integer> arrayToBeCopied) { ArrayList<Integer> copiedArray = new ArrayList<>(); for(int value : arrayToBeCopied) { copiedArray.add(value); } return copiedArray; } String next () { String x1 = ""; //when we move onto the strings of a length one greater than the previous string if (iteration == 0) { powerSet(); } for(int i = 0; i < length; i++) { if(setForLength.get(iteration).contains(i)) { x1 += "b"; } else { x1 += "a"; } } iteration++; if (iteration == Math.pow(2, length) || length == 0) { length++; iteration = 0; } return x1; } } public class Main { public static void main(String[] args) { sigmaStar s = new sigmaStar(); for(int i = 0; i < 220; i++) { System.out.println("STRING " + (i + 1) + " : " + s.next()); } } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more