Easy c++ backtracking


#1

Comment body gobool palindrome(string s)
{
if(s.length()==1)return true;
int i=0,j=s.length()-1;
while(i<j)
{
if(s[i]!=s[j])
return false;
i++;
j–;
}
return true;
}
void solve(vector<vector>& v,string A,vector& op)
{
if(A.length()==0)
{
v.push_back(op);
return;
}
for(int i=0;i<A.length();i++)
{
string pref=A.substr(0,i+1);
string left=A.substr(i+1);
if(palindrome(pref))
{
op.push_back(pref);
solve(v,left,op);
op.pop_back();
}
}
}
vector<vector > Solution::partition(string A)
{
vector<vector >ans;
vectorop;
solve(ans,A,op);
return ans;
}