// This is the java program for the third problem of the lab test of ESC101 // held on 10th November. // The solution of the remaining problems is not provided since they // are VERY VERY EASY. // // This programs generates all permutations of length L // using characters of input string S. The program is generic enough so that // it does not assume that the input string has all distinct characters. // Each permutation MUST ensure that the multiplicity of any character in it // is less than or equal to the multiplicity of that character in the input // string. // For convenience of the programmer, the input string given is such that // in case a character multiple times in the input string, then all these // occurrences are contiguous. For example, the input can be bbbbaacddeff. // First we create two arrays // A: character array for the set of characters in the string // and array char_count[] so that char_count[i] stores the multiplicity of // character A[i]. // The method for generating permutations is very simple and along the lines // of the program permutation.java discussed in the class. // The method follows easily once you give recursive formulation of // the set of permutations in terms of arrays A and char_count. // Example : if input string is aaaaa and L = 2; // then there is only one output which is aa. // If the string is aaaaaab and L=2, // then there are three outputs aa, ab, ba. And so on.. // class perm_general { public static int different_characters(String S) { int count=1; char previous_char = S.charAt(0); for(int i = 1; i0) { char_count[index] = char_count[index]-1; PermuteS(A, char_count, L-1, S+A[index]); char_count[index] = char_count[index]+1; } } } public static void main(String args[]) { String input_string = args[0]; int L = Integer.parseInt(args[1]); int count = different_characters(input_string); char[] A = new char[count]; int[] char_count = new int[count]; Initialize_arrays(A,char_count,input_string); // for(int i = 0; i