I need to generate all the possible combinations of a given char variables in specified length and RETURN the one that matches my criteria.
So, by searching I found the following solution:
#include <bits/stdc++.h>
void printAllCombinations(char set[], std::string prefix, int sizeofSet, int k) {
if (k == 0) {
std::cout << (prefix) << std::endl;
/*
If(prefix is matched) {
return prefix;
}
*/
return;
}
for (int i = 0; i < sizeofSet; i++) {
std::string newPrefix;
newPrefix = prefix + set[i];
printAllCombinations(set, newPrefix, sizeofSet, k - 1);
}
}
int main() {
char mySet[] = {'a', 'b'};
int lengthOfGeneratedStrings = 2;
printAllCombinations(mySet, "", sizeof(mySet), lengthOfGeneratedStrings);
}
Now, I need to change this void function so I can return the qualified string (prefix) as pointed in the commented part of the code.