【leetcode】1147. Longest Chunked Palindrome Decomposition
题目如下:
Return the largest possible
ksuch that there existsa_1, a_2, ..., a_ksuch that:
- Each
a_iis a non-empty string;- Their concatenation
a_1 + a_2 + ... + a_kis equal totext;- 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:
textconsists 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的更多相关文章
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【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 ...
- 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...
- 【leetcode】409. Longest Palindrome
problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【LeetCode】845. Longest Mountain in Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双数组 参考资料 日期 题目地址:https://l ...
- 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
随机推荐
- Openstack_单元测试
目录 目录 单元测试的原理 单元测试的实现 最后 单元测试的原理 单元测试中的单元可以是一个模块文件, 测试的内容就是模块自身的代码(非导入型代码)是否正确执行. 其中包含了测试代码的正反向逻辑是否正 ...
- IntelliJ IDEA调试时,格式化显示日期变量
格式化前: 格式化后: new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this); 入门常用知识: Ct ...
- java创建多线程实现并行计算任务处理
1.直接上代码一看明白: package multithreadingTest; class fblib extends Thread{ public static Integer fb(Intege ...
- tensorflow2.0 numpy.ndarray 与tenor直接互转
1.代码参考 import numpy as npimport tensorflow as tf a = np.random.random((5,3)) b = np.random.randint(0 ...
- Linux-定时任务-打包与压缩
figure:first-child { margin-top: -20px; } #write ol, #write ul { position: relative; } img { max-wid ...
- JavaScript event对象clientX,offsetX,screenX异同
event对象是JavaScript中最重要的对象之一,他代表了各种事件的状态,在各种事件的事件处理中经常用到,比如键盘活动.鼠标活动等等,其中有几个对象官方说的比较模糊,很难理解,这里有必要通俗化的 ...
- 并发编程时守护进程在pycharm与python shell中的运行结果不同
原代码如下 from multiprocessing import Process import time import random def task(name): print('%s is run ...
- 搜索专题: HDU1312Red and Black
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU1688-POJ3463-Sightseeing(求次短路的条数)
题意 求出最短路和次短路的条数,当次短路比最短路长度小1时,输出条数之和,反之输出最短路条数. 题解 dis1[],cnt1[],dis2[],cnt2[] 分别表示最短路的长度和条数,次短路的长度 ...
- Paper Reading_Database
最近(以及预感接下来的一年)会读很多很多的paper......不如开个帖子记录一下读paper心得 AI+DB A. Pavlo et al., Self-Driving Database Engi ...