# [Leetcode] palindrome partitioning II (DP)

Source: Internet
Author: User

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"`, Return`1`Since the palindrome partitioning`["aa","b"]`Cocould be produced using 1 cut.

Avoid using recursion to produce inefficient programs. Use the DP Method:

`Class solution {public: int mincut (string s) {If (S. empty () return 0; int n = S. size (); vector <bool> pal (n, vector <bool> (n, false )); // record whether the input vector <int> D (n) is formed between Si and SJ ); // record Si to S end split can form the smallest number of split back to the text for (INT I = n-1; I> = 0; I --) {d [I] = n-i-1; for (Int J = I; j <n; j ++) {If (s [I] = s [J] & (J-I <2 | pal [I + 1] [J-1]) {pal [I] [J] = true; If (j = N-1) d [I] = 0; else if (d [J + 1] + 1 <D [I]) d [I] = d [J + 1] + 1 ;}}} return d [0] ;}};`

Each pair takes into account the O (N ^ 2) number of times, and then only considers the sub-sequence in which the sub-sequence can constitute the Back-to-text, that is, the sub-sequence pal [I] [J] = true can be formed between Si and SJ.

Related Keywords:

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.