I need to generate all possible strings in a specific range.
For example
Upper bound - aaa
Lowe bound - ccc
Also possible cases aab-caa
, hhu - kkk
, but not aaz - ccc
Values should be
aaa
aab
aac
aba
abb
abc
aca
acb
acc
caa
cab
cac
cca
ccb
ccc
I wrote such method but it doesn't work correctly, I have no idea how to do this correctly, it is too hard for me now to catch all possible cases, please help
public static List<String> generateAllPossibleStrings(String start, String end) {
if (start.length() != end.length()) {
return null;
}
List<String> variants = new ArrayList<>();
char startArray[] = start.toCharArray();
char endArray[] = end.toCharArray();
char currentArray[] = Arrays.copyOf(startArray, startArray.length);
variants.add(new String(currentArray));
for (int i = startArray.length - 1; i >= 0; i--) {
while (currentArray[i] != endArray[i]) {
currentArray[i]++;
variants.add(new String(currentArray));
for (int j = startArray.length - 1; j > i; j--) {
while (currentArray[j] != endArray[j]) {
currentArray[j]++;
variants.add(new String(currentArray));
}
currentArray[j] = startArray[j];
}
}
currentArray[i] = startArray[i];
}
System.out.println(Arrays.toString(variants.toArray()));
return variants;
}
For the example above I got
[aaa, aab, aac, aba, abb, abc, aca, acb, acc, baa, bab, bac, bba, bca, caa, cab, cac, cba, cca]
As you can see some values are missing.
Please help to correct this method and make it work correctly or maybe it should be implemented as recursive method.
EXPLANATION
Why aaz - ccc
is not possible because any character in lower bound (ccc
) should be grater that corresponding character in the upper bound (aaz
) in this example z
is grater than c
so this is incorrect.