I need to write a function which finds the possible fixed length combinations of a string. The need is that not all combis are required. For example, if the string is “abcde”, and we need combis of length 3, then the function must return the following:
abc
abd
abe
acd
ace
ade
bcd
bde
bce
cde
and none else. I have been trying for it using recursion but things have not worked out as expected. I have also seen some similar questions but could not get much out of them. Algorithm or code(C, C++, Java), any help is welcome. Thanks.!
Note: The combinations need to be ordered. That is, the characters should follow the same order as in the input string.
I expect it’s possible to do better, but this is what I could come up with quickly.
I used a
Listto preserve order, and had to avoid duplicates awkwardly. Using aSetinstead would allow us to skip theresult.contains(s)check, but a better algorithm might avoid the duplicates in a cleaner way.