[Solved] C++ recursive algorithm for permutation [closed]


Your problem seems to be in the “smallString” function. In that function, OutOfRange is used in s[j]. I put print like

for(i=0,j=0;j<s.length();i++,j++)
{
    if(i==k){j++;}
      cout<<"smallString:: s ::"<<s<<'\t'<<"k::"<<k<<'\n';
    res.push_back(s[j]); //Problem is here...
}

Now Output print is like

smallString:: s ::abc k::0

smallString:: s ::abc k::0

smallString:: s ::bc k::0

smallString:: s ::bc k::1

smallString:: s ::bc k::1

smallString:: s ::b k::0

smallString:: s ::b k::1

smallString:: s ::b k::1
.
.

So, At one point of time it comes as “s ::b k::1” so you are selecting the character at the position 1 from the string “b”.

String is basically an char array which start from 0 to (n-1). For string “b” only 0th position is having character ‘b’. but we are accessing an non-existing position.

So it throws error and start looping continuously from here.

FIX FOR YOUR ISSUE:

for(i=0,j=0;j<s.length();i++,j++)
{
    if(i==k)
    {
        j++;
    }
    else
    {
        cout<<"smallString:: s ::"<<s<<'\t'<<"k::"<<k<<'\n';
        res.push_back(s[j]);
    }
}

1

solved C++ recursive algorithm for permutation [closed]