The code generates the power set of a given string in lexicographic order. It does this by first sorting the characters in the string, then generating all possible permutations of the sorted string, which is equivalent to generating the power set in lexicographic order.
The powerSet function first converts the input string to a character array and sorts it using the Arrays.sort method. It then calls the permutation function with an empty current permutation and an initial index of -1. The permutation function generates all possible permutations of the string starting from the given index, by adding each character to the current permutation and recursively generating permutations for the remaining characters. It then removes the last character from the current permutation and repeats the process with the next character.
The permutation function also prints each permutation it generates. When it reaches the end of the string, it returns without generating any more permutations.
Overall, this code generates the power set of the input string in lexicographic order by generating all permutations of the sorted string.
import java.util.*; class Lexicographic { public static void main(String[] args) { String str = "123"; powerSet(str); } static void permutation(String str, int len, int index, String curPer) { if (index == len) { return; } System.out.println(curPer); for (int i = index + 1; i < len; i++) { curPer += str.charAt(i); permutation(str, len, i, curPer); curPer = curPer.substring(0, curPer.length() - 1); } return; } static void powerSet(String str) { char[] a = str.toCharArray(); Arrays.sort(a); permutation(new String(a), str.length(), -1, ""); } }
1 12 123 13 2 23 3
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions