Nine-chapter algorithm surface test 48 split Palindrome string

Source: Internet
Author: User

Nine Chapters count judges Net-original websitehttp://www.jiuzhang.com/problem/48/
Topics

For a given string, the minimum number of partitions is required to divide the string into substrings, each of which is a palindrome string. such as Abaab, requires at least 1 division, the string is divided into: A|baab, each part is a palindrome string.


Answer

This is a typical dynamic programming problem of partitioning on a string. The general state representation is as follows: F[i] means dividing the substring of the first I character into a minimum number of strings, each string being a palindrome. Then there is the state transfer equation: f[i] = MIN (F[j] + 1, j<i && j+1~i is a palindrome string). Here to determine whether a string is a palindrome string, you can use O (n) judgment, the overall complexity of I * j * To determine the palindrome string = O (n^3). However, this complexity can be further optimized. That is, there is no need to re-determine whether a string is a palindrome every time. Can be a little more space, such as using ispalindrome[i][j] to represent I to J is a palindrome string. It is clear that this array is recursive and can be resolved in O (n^2) time. So the overall time complexity can be reduced to O (n^2)



Reference Program:

http://answer.ninechapter.com/solutions/palindrome-partitioning-ii/



Nine-chapter algorithm surface test 48 split Palindrome string

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.