Recurrsion - Help

I need help trying to figure out this algorithm. I've been stuck for days now The debugger just confuses me more. Can somebody please just comment on one loop so i can see how the internals work. Thanks you much appreciated!

permutaions("nop")


vector<string> permutations(const string& word){

size_t n = word.size();
cout <<"word size " << n << endl;
vector<string> perms;

if(n <= 1)
{
perms.push_back(word);
return perms;
}

for(size_t i = 0; i < n; ++i){
cout <<" i = " << i << endl;
string shortWord = word.substr(0,i) + word.substr(i + 1);


//Call permutaions on the shorter word, f(n-1)

vector<string>shortPerm = permutations(shortWord);
int shortP = shortPerm.size();
cout << "shorts size is "<< shortP << endl;

//Combine ommitted letter with permutations on shorter word

for(size_t j = 0, m = shortPerm.size(); j < m; ++j){
cout << " j = " << j << " i = " << i << endl;
string longWord = word[i] + shortPerm[j];
cout << word[i] << " " << shortPerm[j] << endl;
perms.push_back(longWord);

}

}
return perms;
}
Topic archived. No new replies allowed.