Leetcode & mdash; Palindrome solution report

Source: Internet
Author: User

1. Description

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"]  ]
That is to say, if a string is given to identify all possible classifications, each of which is a return string. Set this partition function to partition_index (string s, int begin, int end)

2. Solutions

Of course, the brute force method can solve this problem. For a string with a length of n, there are 2n-1 division methods. You only need to determine whether each division meets the conditions, although I don't know whether the brute force can be used in the system, it is obviously not a normal programmer. However, the brute force method will give us some inspiration. We will analyze why there are 2n-1 division methods in the brute force method.

Of course, this is also easy to analyze. For strings with n length, s0s1... Sk... Sn-1, in sk and SK-1 between we can choose to cut off and not cut off, however, we can be based on the idea of a recursive idea, that is, for the string sbeginsbegin + 1... Sbegin + k... Sn-1, all possible conditions can be divided into two categories:

(1) There is a partition between sbegin and sbegin + 1. In this case, you only need to connect sbegin and partition_index (s, begin + 1, n-1 ).

(2) There is no partition between sbegin and sbegin + 1. In this case, you need to find a return string containing at least sbegin and sbegin + 1. If you can find it, for example, sbegin... Sbegin + k is a return string. Connect the return string with partition_index (s, k + 1, n-1). Note, it is possible to find more than one input string containing s0 and s1, so the number of input strings may be more than the number of classes. If no input string is found, an empty vector is returned.

3. Code

                    
                     
                      
                       
                        
                         
                          
                           
                            
                             
                              
                               
                                
                                 
                                  
                                   
                                    
                                     
                                      
                                       
                                        
                                         
                                          
                                           
                                            
                                             
                                              
                                               
                                                
                                                 
                                                  
                                                   
                                                    
                                                     
                                                      
                                                       
                                                        
                                                         
                                                          
                                                           
                                                            
                                                             
                                                              
                                                               
                                                                
                                                                 
                                                                  
                                                                   
                                                                    
                                                                     
                                                                      
                                                                       
                                                                        
                                                                         
                                                                          
                                                                           
                                                                            
                                                                             
                                                                              
                                                                               
                                                                                
                                                                                 
                                                                                  
                                                                                   
                                                                                    
                                                                                     
                                                                                      
                                                                                       
                                                                                        
                                                                                         
                                                                                          
                                                                                           
                                                                                            
                                                                                             
                                                                                              
                                                                                               
                                                                                                
                                                                                                 
                                                                                                  
                                                                                                   
                                                                                                    
                                                                                                     
                                                                                                      
                                                                                                       
                                                                                                        
                                                                                                         
                                                                                                          
                                                                                                           
                                                                                                            
                                                                                                             
                                                                                                              
                                                                                                               
                                                                                                                
                                                                                                                 
                                                                                                                  
                                                                                                                   
                                                                                                                    
                                                                                                                     
                                                                                                                      
                                                                                                                       
                                                                                                                        
                                                                                                                         
                                                                                                                          
                                                                                                                           
                                                                                                                            
                                                                                                                             
                                                                                                                              
                                                                                                                               
                                                                                                                                
                                                                                                                                 
                                                                                                                                  
                                                                                                                                                   

 

In addition, the code can be simplified. For example, the Code of the connection result can be encapsulated into a function.

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.