[Leetcode 131, Medium] Palindrome Partitioning

来源:互联网 发布:mac chm阅读器 最好的 编辑:程序博客网 时间:2024/06/07 11:55

Problem:

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

Return all possible palindrome partitioning of s.

For example, given s = "aab",
Return

  [    ["aa","b"],    ["a","a","b"]  ]

Analysis:


Solution:

C++:

bool IsPalindrome(const string& str) {        for(int begin = 0, end = str.size() - 1; begin < end; ++begin, --end)            if(str[begin] != str[end])                return false;        return true;    }        void PopulatePalindromes(string str, vector<string>& partition, vector<vector<string> >& candidates)    {        for(int i = 1; i <= str.size(); ++i) {            string prefix = str.substr(0, i);            if(IsPalindrome(prefix)) {                partition.push_back(prefix);                if(i == str.size())                    candidates.push_back(partition);                else                    PopulatePalindromes(str.substr(i), partition, candidates);                partition.erase(partition.end() - 1, partition.end());            }        }    }        vector<vector<string> > partition(string s) {        vector<vector<string> > candidates;        vector<string> partition;        PopulatePalindromes(s, partition, candidates);        return candidates;    }
Java:


Python:



0 0