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.
|