题目如下:

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. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_3 Mybatis的CRUD-修改和删除操作

    增加更新操作 更新用户的配置 测试类 删除的操作 这里的parpameterType值可以是:Integer.INTEGER.int.java.lang.Integer 讲到typeAliases标签 ...

  2. HTTP学习记录:三、状态码

    学习资源主要为:@小坦克HTTP相关博客 常见的HTTP状态码: 200--请求成功: 302--重定向: 304--Not Modified:表示上次的文档已经被缓存了,还可以继续使用: 400-- ...

  3. SAP屏幕事件的控制

    1. INITALIZATION事件 该事件在屏幕未显示之前执行,对程序设置值及屏幕元素进行初始化赋值. REPORT  Y001. PARAMETERS QUAL_DAY TYPE D DEFAUL ...

  4. Linux iptables 防火墙常用规则

    iptables 安装 yum install iptables iptables 规则清除 iptables -F iptables -X iptables -Z 开放指定的端口允许本地回环接口(即 ...

  5. Java多线程学习——知识点积累

    开启多线程时,每一个线程都拥有自己的工作空间,每个工作空间都单独的和主存打交道. 并发的概念:多个线程同时操作同一个对象 当产生并发时,如果从工作空间写入数据到内存的线程时间片用完了,其他线程再从主存 ...

  6. django groupby 用法

  7. 多线程18-QueueUserWorkItem

        ));         }         ;             ;             ));             ThreadPool.QueueUserWorkItem(A ...

  8. Spring Boot 深度调优,6得飞起~

    项目调优 作为一名工程师,项目调优这事,是必须得熟练掌握的事情. 在SpringBoot项目中,调优主要通过配置文件和配置JVM的参数的方式进行. 一.修改配置文件 关于修改配置文件applicati ...

  9. cqoj921E整数匹配

    这是一个贪心题,把我坑的好惨,忘还原得70.上午被卡得,, 首先给出长度为n的一组数,可以两两配对相乘也可以进行相加,问怎样才可以使总和最大?那么可以显然看出来,当这个数为0或1时,我们要相加.其余进 ...

  10. 使用redis+flask维护动态代理池

    在进行网络爬虫时,会经常有封ip的现象.可以使用代理池来进行代理ip的处理. 代理池的要求:多站抓取,异步检测.定时筛选,持续更新.提供接口,易于提取. 代理池架构:获取器,过滤器,代理队列,定时检测 ...