Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

Example:

Input: "aab"
Output:
[
  ["aa","b"],
  ["a","a","b"]
]

code

class Solution
{
public:
    vector<vector<string>> partition(string s)
    {
        if(s.empty())
            return {};

        vector<vector<string>> res;
        vector<string> tmp;
        partitionCore(s,0,tmp,res);
        return res;
    }
private:
    void partitionCore(const string &s,int start,vector<string> &tmp,vector<vector<string>> &res)
    {
        if(start==s.size())
        {
            res.emplace_back(tmp);
            return ;
        }

        for(int i=start;i<s.size();++i)
        {
            if(!isPalindrome(s,start,i))
                continue;

            tmp.emplace_back(s.substr(start,i-start+1));
            partitionCore(s,i+1,tmp,res);
            tmp.pop_back();
        }
    }
    bool isPalindrome(const string &s,int start,int end)
    {
        while(start<end)
            if(s.at(start++)!=s.at(end--))
                return false;
        return true;
    }
};

 

上一篇:C# xml转换为对象


下一篇:打造美好家居数字化转型标杆,亚士创能与阿里云达成战略合作