题目如下:

Given a string s, a k duplicate removal consists of choosing k adjacent and equal letters from s and removing them causing the left and the right side of the deleted substring to concatenate together.

We repeatedly make k duplicate removals on s until we no longer can.

Return the final string after all such duplicate removals have been made.

It is guaranteed that the answer is unique.

Example 1:

Input: s = "abcd", k = 2
Output: "abcd"
Explanation: There's nothing to delete.

Example 2:

Input: s = "deeedbbcccbdaa", k = 3
Output: "aa"
Explanation:
First delete "eee" and "ccc", get "ddbbbdaa"
Then delete "bbb", get "dddaa"
Finally delete "ddd", get "aa"

Example 3:

Input: s = "pbbcggttciiippooaais", k = 2
Output: "ps" 

Constraints:

  • 1 <= s.length <= 10^5
  • 2 <= k <= 10^4
  • s only contains lower case English letters.

解题思路:本题解法不难,利用入栈出栈的思路即可。但有几点注意一下,一是只有长度为k的连续出现的相同的字符才能消除,如果有(k+1)个字符a的话,只能消除k个,留下剩余的一个a;同时注意消除后的相同字符的合并。

代码如下:

class Solution(object):
def removeDuplicates(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
stack = []
s += '#'
last_char = None
continuous = 1
for i in s:
if last_char == None:
last_char = i
elif last_char == i:
continuous += 1
else:
stack.append([last_char,continuous])
last_char = i
continuous = 1
#print stack
for i in range(len(stack)-1,-1,-1):
if stack[i][1] >= k:
if stack[i][1] % k == 0:
del stack[i]
else:
stack[i][1] = stack[i][1] % k
if i < len(stack) - 1 and stack[i][0] == stack[i+1][0]:
stack[i][1] += stack[i+1][1]
del stack[i+1]
if i < len(stack) and stack[i][1] >= k:
if stack[i][1] % k == 0:
del stack[i]
else:
stack[i][1] = stack[i][1] % k
res = ''
for char,count in stack:
res += char*count
return res

【leetcode】1209. Remove All Adjacent Duplicates in String II的更多相关文章

  1. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

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

  2. 【leetcode】1047. Remove All Adjacent Duplicates In String

    题目如下: Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent a ...

  3. 【Leetcode_easy】1047. Remove All Adjacent Duplicates In String

    problem 1047. Remove All Adjacent Duplicates In String 参考 1. Leetcode_easy_1047. Remove All Adjacent ...

  4. LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)

    1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...

  5. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  6. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  7. LeetCode 1047. Remove All Adjacent Duplicates In String

    1047. Remove All Adjacent Duplicates In String(删除字符串中的所有相邻重复项) 链接:https://leetcode-cn.com/problems/r ...

  8. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  9. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

随机推荐

  1. mybatis 动态SQL .2

    目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when,otherwise) 语句 5.动态SQL:tri ...

  2. Service Mesh体验

    前言# 计算机软件技术发展到现在,软件架构的演进无不朝着让开发者能够更加轻松快捷地构建大型复杂应用的方向发展.容器技术最初是为了解决运行环境的不一致问题而产生的,随着不断地发展,围绕容器技术衍生出来越 ...

  3. LeetCode.989-数组形式的整数做加法(Add to Array-Form of Integer)

    这是悦乐书的第371次更新,第399篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第233题(顺位题号是989).对于非负整数X,X的数组形式是从左到右顺序的数字数组.例 ...

  4. Express全系列教程之(十一):渲染ejs模板引擎

    一.简介 相比于jade模板引擎,ejs对原HTML语言就未作出结构上的改变,只不过在其交互数据方面做出了些许修改,相比于jade更加简单易用.因此其学习成本是很低的.您也可参考ejs官网:https ...

  5. GIS学习之栅格数据

    栅格数据用一个规则格网来描述与每一个格网单元位置相对应的空间现象特征的位置和取值.在概念上,空间现象的变化由格网单元值的变化来反映.地理信息系统中许多数据都用栅格格式来表示.栅格数据在许多方面是矢量数 ...

  6. 【VS开发】【图像处理】RGB Bayer Color分析

    RGB Bayer Color分析 Bayer色彩滤波阵列 拜耳色彩滤波阵列(Bayer Color Filter Array,CFA)是非常有名的彩色图片的数字采集格式.色彩滤波器的模式如上图所示, ...

  7. linux查看cd/dvd驱动器的设备信息

    在linux下,如何来查看系统里的CD-ROM或者DVD驱动器的设备名呢? 你可以输入下面的命令来查看当前系统下的光盘驱动器信息: 1.使用dmesg命令来查看当前的硬件是否被linux内核正确的识别 ...

  8. 性能库 Oracle数据库 连接不上问题的解决

    今天性能库的centos 上面的oracle18c 数据库连接不上了 这里进行了一次简单处理 针对不同问题进行说明: 1. 实例时 blocked 的状态 问题现象: LSNRCTL> stat ...

  9. Spring(六)--Spring配置文件之间的关系

    Spring配置文件之间的关系 1.需要的实体类 2.需要的xml文件 3.测试类 未完待续!!!

  10. MySQL表存在外键关系无法清空数据的解决方案

    先 SET FOREIGN_KEY_CHECKS=0; 然后delete删除,再 SET FOREIGN_KEY_CHECKS=1;