Simple c++ standard backtrack solution


#1

bool palindrome(string s)
{

for(int i=0;i<s.length();i++)
{
    if(s[i]!=s[s.length()-i-1])
    {
        return false;
    }
    
}
return true;

}
void dfs(vector<vector>&sol,vector&res,string A,int start)
{
if(!res.empty()&&!palindrome(res.back()))
{
return;
}
if(start>=A.size())
{
sol.push_back(res);
}
for(int i=1;i<=A.length()-start;i++)
{
res.push_back(A.substr(start,i));
dfs(sol,res,A,start+i);
res.pop_back();

}

}
vector<vector > Solution::partition(string A)
{
vector<vector>sol;
vectorres;
int start=0;
dfs(sol,res,A,start);
return sol;
}


#2

C++