题目如下:

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

解题思路:本题不算太难,我的方法是贪心算法+双指针。首先引入head和tail两个变量,分别等于text[0]和text[-1]。如果head等于tail,表示这两者可以组成回文段的两部分,再令head等于text[1],tail等于text[-2];如果两者不相等,令head = head + text[0],tail = text[-2] + tail,直到head 等于tail为止。原则就是每遇到head等于tail的情况,表示这两段是回文段的一部分,重置head 和tail的值。

代码如下:

class Solution(object):
def longestDecomposition(self, text):
"""
:type text: str
:rtype: int
"""
res = 0
head_inx = 0
tail_inx = len(text) - 1
head = ''
tail = ''
while head_inx <= tail_inx and head_inx < len(text) and tail_inx >= 0:
if head == '' and tail == '':
head = text[head_inx]
tail = text[tail_inx]
head_inx += 1
tail_inx -= 1
elif head == tail:
res += 2
head = text[head_inx]
tail = text[tail_inx]
head_inx += 1
tail_inx -= 1
else:
#head_inx += 1
#tail_inx -= 1
head = head + text[head_inx]
tail = text[tail_inx] + tail
head_inx += 1
tail_inx -= 1
res += 2 if head == tail and head_inx - len(head) != tail_inx + len(tail) else 1
return res if res != 0 else 1

【leetcode】1147. Longest Chunked Palindrome Decomposition的更多相关文章

  1. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  2. 【LeetCode】159. Longest Substring with At Most Two Distinct Characters

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...

  3. 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...

  4. 【leetcode】409. Longest Palindrome

    problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...

  5. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  6. 【LeetCode】845. Longest Mountain in Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双数组 参考资料 日期 题目地址:https://l ...

  7. 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...

  8. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  9. 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...

随机推荐

  1. 旅游局nginx配置

    #user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log notice;#error_l ...

  2. WinFrom控件双向绑定

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. ZABBIX_PROXy

    ProxyMode=0 Server=172.19.30.50 ServerPort=10051 Hostname=172.17.26.102 ListenPort=10051 LogFile=/va ...

  4. Mailx安装与使用

    1.卸载sendmail与postfix yum -y install mailx 2.安装mailx yum -y remove sendmail postfix 3.配置mail.rc vim / ...

  5. C语言第八周编程作业

        这个作业属于哪个课程 C语言程序设计 这个作业要求在哪 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass3-2018/hom ...

  6. C语言I-博客作业05

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 C语言1博客作业05 我在这个课程的目标是 学会运用函数编代码 这个作业在那个具体方面帮助我实现目标 写C语言作业的时候,编代码的实践中 ...

  7. [转帖]是时候深入了解Linux的系统结构

    是时候深入了解Linux的系统结构   http://os.51cto.com/art/201905/596011.htm linux的体系结果 其实自己也知道 linus 做了一个 kernel 大 ...

  8. Windows Server 2012 上安装 dotNET Framework v3.5

    Windows Server 2012不能直接运行dotNET Framework v3.5安装程序进行安装,系统提供通过服务器管理器的添加功能和角色向导进行安装. 安装的前几个步骤再这里略去,在默认 ...

  9. arm初识

    一.CPU从指令集角度分类 1.1. CISC  (X86) 1.1. 1. CISC CPU 指complex instruction set computer复杂指令集CPU 1.1.2. CIS ...

  10. [多校联考2019(Round 5 T2)]蓝精灵的请求(二分图染色+背包)

    [多校联考2019(Round 5)]蓝精灵的请求(二分图染色+背包) 题面 在山的那边海的那边住着 n 个蓝精灵,这 n 个蓝精灵之间有 m 对好友关系,现在蓝精灵们想要玩一个团队竞技游戏,需要分为 ...