[Swift]LeetCode1147. 段式回文 | Longest Chunked Palindrome Decomposition
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/11297775.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強烈建議點擊原文地址閱讀!支持作者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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
段式回文 其實與 一般回文 類似,只不過是最小的單位是 一段字符?而不是 單個字母。
舉個例子,對于一般回文 "abcba" 是回文,而 "volvo" 不是,但如果我們把?"volvo" 分為 "vo"、"l"、"vo" 三段,則可以認為 “(vo)(l)(vo)” 是段式回文(分為 3 段)。?
給你一個字符串?text,在確保它滿足段式回文的前提下,請你返回?段?的?最大數量?k。
如果段的最大數量為?k,那么存在滿足以下條件的?a_1, a_2, ..., a_k:
- 每個?a_i?都是一個非空字符串;
- 將這些字符串首位相連的結果?a_1 + a_2 + ... + a_k?和原始字符串?text?相同;
- 對于所有1 <= i <= k,都有?a_i = a_{k+1 - i}。?
示例 1:
輸入:text = "ghiabcdefhelloadamhelloabcdefghi" 輸出:7 解釋:我們可以把字符串拆分成 "(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)"。示例 2:
輸入:text = "merchant" 輸出:1 解釋:我們可以把字符串拆分成 "(merchant)"。示例 3:
輸入:text = "antaprezatepzapreanta" 輸出:11 解釋:我們可以把字符串拆分成 "(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)"。示例 4:
輸入:text = "aaa" 輸出:3 解釋:我們可以把字符串拆分成 "(a)(a)(a)"。?提示:
- text?僅由小寫英文字符組成。
- 1 <= text.length <= 1000
8ms 1 class Solution { 2 func longestDecomposition(_ text: String) -> Int { 3 var result = 0 4 var chars = Array(text) 5 var n = text.count 6 var l = "" 7 var r = "" 8 for i in 0..<n { 9 l += String(chars[i]) 10 r = String(chars[n-i-1]) + r 11 if l == r { 12 result += 1 13 l = "" 14 r = "" 15 } 16 } 17 return result 18 } 19 }
Runtime:?12 ms
Memory Usage:?20.6 MB 1 class Solution { 2 func longestDecomposition(_ text: String) -> Int { 3 let n:Int = text.count 4 for i in 0..<(n/2) 5 { 6 if text.subString(0, i + 1) == (text.subString(n - 1 - i, n)) 7 { 8 return 2 + longestDecomposition(text.subString(i + 1, n - 1 - i)) 9 } 10 } 11 return (n == 0) ? 0 : 1 12 } 13 } 14 extension String { 15 // 截取字符串:指定索引和字符數 16 // - star: 開始索引 17 // - end: 結束索引 18 func subString(_ start:Int,_ end:Int) -> String { 19 let start = self.index(self.startIndex, offsetBy: start) 20 let end = self.index(self.startIndex, offsetBy: end) 21 return String(self[start..<end]) 22 } 23 }12ms? 1 class Solution { 2 func longestDecomposition(_ text: String) -> Int { 3 let chars = Array(text) 4 var l = chars.startIndex 5 var r = chars.endIndex - 1 6 let mid = (l + r + 1) / 2 7 var ans = 0 8 9 while l < r { 10 let range = 0..<mid-l 11 var next = range.upperBound 12 for i in range where chars[l...l+i] == chars[r-i...r] { 13 next = i + 1 14 ans += 2 15 if l + i + 1 == r - i { 16 return ans 17 }else{ 18 break 19 } 20 } 21 l += next 22 r -= next 23 } 24 return ans + 1 25 } 26 }
16ms
1 class Solution { 2 func longestDecomposition(_ text: String) -> Int { 3 let s = Array(text) 4 var result = 0 5 6 let end = s.endIndex - 1 7 var i = s.startIndex 8 var j = s.startIndex 9 10 //ghiabcdefhelloadamhelloabcdefghi 11 while i <= end{ 12 while j <= end { 13 let range1 = i...(i+j) 14 let range2 = (end-i-j)...(end-i) 15 j += 1 16 guard s[range1] == s[range2] else { continue } 17 result += 1 18 break 19 } 20 21 i += j 22 j = 0 23 } 24 return result 25 } 26 }?
轉載于:https://www.cnblogs.com/strengthen/p/11297775.html
總結
以上是生活随笔為你收集整理的[Swift]LeetCode1147. 段式回文 | Longest Chunked Palindrome Decomposition的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机操作系统-3-存储管理
- 下一篇: 长安汽车:自主品牌新能源 7 月销量 3