您的位置:首页 > 其它

[Leetcode 131, Medium] Palindrome Partitioning

2015-02-02 13:03 387 查看
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:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: