Leetcode algorithm Programming-palindrome Partitioning

Source: Internet
Author: User

1. Topics
Given Astrings, partition s such that every substring of the partition isa palindrome. Return all possible palindrome partitioning of s.for example, given S="AaB", Return [["AA","b"],    ["a","a","b"]  ]

Topic Explanation:

Specifies the string s, which returns all possible substrings of s, each of which must be a palindrome (a string that is the same as the Read and rewind), as shown in.

Analysis

If you think about it carefully, you will find that the problem is to go through all the possibilities and find the right solution. You can first try to write the algorithm, the problem will test the basic skills of recursion.

This paper first introduces the backtracking algorithm, and the backtracking algorithm is called heuristic method, which is a method to search the solution of the problem systematically. The basic idea of backtracking algorithm is: Go forward from a road, can enter into, can not enter the back, change a road and try again. The general steps for solving problems with backtracking algorithms are:

1, define a solution space, it contains the solution of the problem.

2, the use of suitable search methods to organize the solution space.

3, the use of depth-first method to search the solution space.

4, use the Limit function to avoid moving to the sub-space that cannot produce the solution.

The solution space of the problem is usually generated dynamically in the process of searching the solution of the problem, which is an important feature of backtracking algorithm.

Source:

classSolution { Public:    //Check if a string is a palindrome    BOOLCheck_palindrome (strings) {intSize =s.size ();  for(inti =0; I < size/2; i + +)        {            if(S[i]! = s[size-i-1])            {                return false; }        }        return true; }    voidFind_partition_sln (strings, Vector<string> &sln, Vector<vector<string> > &result) {        //Boundary End Condition        if(s.size () = =0)        {            //to get here, it shows that a possible solution has been found, and the preceding substrings are all eligible.Result.push_back (SLN); }        intSize =s.size (); //separates a string from the first position to the last position, in turn//and in each case, using deep search to find the right solution         for(inti =1; I <= size; i + +)        {            stringFront; stringremain; Front.assign (s),0, i); Remain.assign (S, I, size-i); if(Check_palindrome (front)) {//Front Meet the conditionsSln.push_back (front); //looking for a workable solution that begins with front, depth firstFind_partition_sln (remain, sln, result); //start the next lookup againSln.pop_back (); }}} vector<vector<string> > Partition (strings) {vector<string>sln; Vector<vector<string> >result;        Find_partition_sln (S, sln, result); returnresult; }};

Leetcode algorithm Programming-palindrome Partitioning

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.