Leetcode: palindrome Partition

Source: Internet
Author: User

Leetcode: palindrome Partition

Given a stringS, PartitionSSuch that every substring of the partition is a palindrome.

Return all possible palindrome partitioningS.

For example, givenS="aab",
Return

[["AA", "B"], ["A", "A", "B"]
Address: https://oj.leetcode.com/problems/palindrome-partitioning/
Algorithm: it can be solved through dynamic planning. A two-dimensional array DP is used to store solutions to all sub-problems. The one-dimensional array DP [I] is used to store 0 ~ All solutions of the I string, each of which is marked by the start position of the last input. For example, for the above string "AAB ",
DP [0] = {0}, DP [1] = {0, 1}, DP [2] = {2 }. In this way, the recursive method can be used to construct the 0 ~ string for all the values of DP [n-1] [n-1] [J] In DP [n-1 ~ DP [n-1] [J]-1, then add DP [n-1] [J] ~ N-1
String. Code:
 1 class Solution { 2 public: 3     vector<vector<string> > partition(string s) { 4         int n = s.size(); 5         if (n < 1)  return vector<vector<string> >(); 6         vector<vector<int> > dp(n); 7         dp[0].push_back(0); 8         for (int i = 1; i < n; ++i){ 9             if(isPalindrome(s.substr(0,i+1))){10                 dp[i].push_back(0);11             }12             dp[i].push_back(i);13             for (int j = i-2; j >= 0; --j){14                 if(isPalindrome(s.substr(j+1,i-j))){15                     dp[i].push_back(j+1);16                 }17             }18         }19         return constructResult(s,dp,n);20     }21     bool isPalindrome(const string &s){22         int len = s.size();23         int n = len / 2;24         int i = 0;25         while(i < n && s[i] == s[len-1-i])    ++i;26         return i == n;27     }28     vector<vector<string> > constructResult(string &s, vector<vector<int> > &dp,int n){29         if (n < 1){30             return vector<vector<string> >();31         }32         vector<int>::iterator it = dp[n-1].begin();33         vector<vector<string> > result;34         for (; it != dp[n-1].end(); ++it){35             if (*it == 0){36                 vector<string> temp1;37                 temp1.push_back(s.substr(0,n));38                 result.push_back(temp1);39                 continue;40             }41             vector<vector<string> >temp2 = constructResult(s,dp,*it);42             vector<vector<string> >::iterator str_it = temp2.begin();43             for(; str_it != temp2.end(); ++str_it){44                 str_it->push_back(s.substr(*it,n-(*it)));45                 result.push_back(*str_it);46             }47         }48         return result;49     }50 };

Question 2:

Given a stringS, PartitionSSuch that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioningS.

For example, givenS="aab",
Return1Since the palindrome partitioning["aa","b"]Cocould be produced using 1 cut.

Address: https://oj.leetcode.com/problems/palindrome-partitioning-ii/

Algorithm: Dynamic Programming is also used to solve the problem, but this time we only need to use one-dimensional array DP to store all sub-problems. DP [I] indicates the string 0 ~ The minimum cut used by I, DP [I + 1] = min {DP [J-1] | 0 = <j <= I + 1 and the string J ~ I + 1 is a reply }. Code:

 1 class Solution { 2 public: 3     int minCut(string s) { 4         int n = s.size(); 5         if(n < 1)   return 0; 6         vector<int> dp(n); 7         dp[0] = 0; 8         for(int i = 1; i < n; ++i){ 9             if(isPalindrome(s.substr(0,i+1))){10                 dp[i] = 0;11                 continue;12             }13             int min_value = n;14             for(int j = i-1; j >= 0; --j){15                 if(dp[j]+1 < min_value){16                     if(isPalindrome(s.substr(j+1,i-j))){17                         min_value = dp[j] + 1;18                     }19                 }20             }21             dp[i] = min_value;22         }23         return dp[n-1];24     }25     bool isPalindrome(const string &s){26         int len = s.size();27         int n = len / 2;28         int i = 0;29         while(i < n && s[i] == s[len-1-i])    ++i;30         return i == n;31     }32 };

 



Leetcode: palindrome Partition

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.