This program is to generate all possible strings from given char sequence with given set but it take much time, how to reduce memory usage and speed up this prodram to work in multi task with out generate the same string twice??
public class LapTest {
static int q=0;
public static void main(String[] args) {
System.out.println("First Test");
char set1[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int k = 4;
printAllKLength(set1, k);
//Do some logic
}
// The method that prints all possible strings of length k. It is
// mainly a wrapper over recursive function printAllKLengthRec()
static void printAllKLength(char set[], int k) {
int n = set.length;
printAllKLengthRec(set, "", n, k);
}
// The main recursive method to print all possible strings of length k
static void printAllKLengthRec(char set[], String prefix, int n, int k) {
// Base case: k is 0, print prefix
if (k == 0) { q++;
System.out.println(prefix);
if (prefix.equals("hkka")) {
System.exit(0);
}
return;
}
// One by one add all characters from set and recursively
// call for k equals to k-1
for (int i = 0; i < n; ++i) {
// Next character of input added
String newPrefix = prefix + set[i];
// k is decreased, because we have added a new character
printAllKLengthRec(set, newPrefix, n, k - 1);
}
}
}
ExecutorService. There is a tutorial on concurrency you can work through.