[Swift]LeetCode1147. 段式回文 | Longest Chunked Palindrome Decomposition

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: Shan Wing Chi ( shanqingyongzhi)
➤ blog Park address: San-ching Wing Chi ( https://www.cnblogs.com/strengthen/ )
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address:
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Return the largest possible k such that there exists a_1, a_2, ..., a_k such that:

  • Each a_i is a non-empty string;
  • Their concatenation a_1 + a_2 + ... + a_k is equal to text;
  • For all 1 <= i <= k,  a_i = a_{k+1 - i}

Example 1:

Input: text = "ghiabcdefhelloadamhelloabcdefghi"
Output: 7
Explanation: We can split the string on "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)".

Example 2:

Input: text = "merchant"
Output: 1
Explanation: We can split the string on "(merchant)".

Example 3:

Input: text = "antaprezatepzapreanta"
Output: 11
Explanation: We can split the string on "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)".

Example 4:

Input: text = "aaa"
Output: 3
Explanation: We can split the string on "(a)(a)(a)". 

Constraints:

  • text consists only of lowercase English characters.
  • 1 <= text.length <= 1000

In fact, similar to the general segment palindrome palindrome, but is the smallest unit was a character rather than a single letter.

For example, for the average palindrome " abcba" it is a palindrome, and " volvo" No, but if we put " volvo" into " vo", " l", " vo" three-stage, it can be considered " (vo)(l)(vo)" is a palindrome Segment (points 3 segments). 

Give you a string  text, the premise of ensuring it meets the stage palindrome, please return the maximum number of segments  k.

If the maximum number of segments  k, the presence of the following conditions are met  a_1, a_2, ..., a_k:

  • Each  a_i is a non-empty string;
  • The results of these strings are connected the first  a_1 + a_2 + ... + a_k and the original strings  text the same;
  • For all 1 <= i <= k, there is  a_i = a_{k+1 - i}

Example 1:

Input: text = "ghiabcdefhelloadamhelloabcdefghi" 
Output: 7 
Explanation: We can split into strings "(ghi) (abcdef) ( hello) (adam) (hello) (abcdef) (ghi)".

Example 2:

Input: text = "merchant" 
Output: 1 
Explanation: We can split into strings "(merchant)".

Example 3:

Input: text = "antaprezatepzapreanta" 
Output: 11 
Explanation: We can split into strings "(a) (nt) ( a) (pre) (za) (tpe) (za) (pre) (a) (nt ) (a) ".

Example 4:

Input: text = "aaa" 
Output: 3 
Explanation: We can split into strings "(a) (a) (  a)".

prompt:

  • text English only lowercase characters.
  • 1 <= text.length <= 1000

Guess you like

Origin www.cnblogs.com/strengthen/p/11297775.html