Here’s a problem thats got me stumped (solution wise):
Given a str S, apply character mappings Cm = {a=(m,o,p),d=(q,u),...} and print out all possible combinations using C or C++.
The string can be any length, and the number of character mappings varies, and there won’t be any mappings that map to another map (thus avoiding circular dependencies).
As an example: string abba with mappings a=(e,o), d=(g,h), b=(i) would print:
abba,ebba,obba,abbe,abbo,ebbe,ebbo,obbe,obbo,aiba,aiia,abia,eiba,eiia,......
Definitely possible, not really difficult… but this will generate lots of strings that’s for sure.
The first thing to remark is that you know how many strings it’s going to generate beforehand, so it’s easy to do some sanity check 🙂
The second: it sounds like a recursive solution would be easy (like many traversal problems).
And here is the output:
Yeah, rather lengthy, but there’s a lot that does not directly participate to the computation (initialization, checks, printing). The core methods is
nextwhich implements the recursion.