I'm having difficult time while trying to produce all possible strings of a given character set. Let S
be set of symbols. I need to process all possible combinations of S
in length n
. For example if S={'a','b','+','-'}
and n=4
the algorithm should process following sequences:
aaaa
aaab
abab
+aa-
// And all other sequences in the universe
Currently my algorithm is a non-efficient recursive algorithm described below. I have two question:
- Is there a faster algorithm?
- Is there a parallel algorithm to solve this problem?
Current implementation: (Simplified)
void workhorse(vector<char> &input, vector<char>::iterator i)
{
if(i==inputs.end()) {
// process the input
return;
}
else {
for( const auto& symbol : S) {
*i=symbol;
workhorse(input, i+1);
}
}
}