Palindrome partitioning [leetcode] DFS and DP Solutions

Source: Internet
Author: User

The first method is DFS, which finds all possible prefixes and recursively calls partition (remaining string)

Complexity: O (2 ^ N)

The Code is as follows:

    vector<vector<string>> partition(string s) {        vector<vector<string>> res;        vector<string> patition;        if (s.size() == 0) return res;        partition(s, patition, res);        return res;    }        void partition(string s, vector<string>& patition, vector<vector<string>>& res) {        for (int i = 1; i < s.size(); i++)        {            if (isPalindrome(s.substr(0, i)))            {                patition.push_back(s.substr(0, i));                partition(s.substr(i), patition, res);                patition.pop_back();            }        }        if (isPalindrome(s))        {            patition.push_back(s);            res.push_back(patition);            patition.pop_back();        }    }        bool isPalindrome(string s)    {        int l = 0, r = s.size() - 1;        while (l <= r)        {            if (s[l] != s[r]) return false;            l++;            r--;        }        return true;    }

The second method is DP.

From https://oj.leetcode.com/discuss/9623/my-java-dp-only-solution-without-recursion-o-n-2

Res (I) represents all decomposition methods for S [0... I-1]

Ispalin (I, j) indicates whether s [I... J] is a return string.

Ispalin (I, j) = true if I = J or (s [I] = s [J] And ispalin (I + 1, J-1 )) or (s [I] = s [J] And I + 1 = J)

Res (I) = res (j) + s [j... I-1] If ispalin (J, I-1)

    vector<vector<string>> partition(string s) {        int size = s.size();        vector<vector<vector<string>>> res(size + 1);        res[0].push_back(vector<string>(0));        vector<vector<bool>> isPalin(size + 1, vector<bool>(size + 1, false));                for (int i = 0; i < size; i++)        {            for (int j = 0; j <= i; j++)            {                if (i == j || s[i] == s[j] && (j + 1 == i || isPalin[j + 1][i - 1]))                {                    isPalin[j][i] = true;                    for (int p = 0; p < res[j].size(); p++)                    {                        vector<string> prefix = res[j][p];                        prefix.push_back(s.substr(j, i - j + 1));                        res[i + 1].push_back(prefix);                    }                }            }        }        return res[size];    }


Palindrome partitioning [leetcode] DFS and DP Solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.