题目如下:

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. python学习之函数(二)

    4.4.6 动态传参 动态传参是针对形参而言 1.动态位置参数 ​ 在静态位置参数时,我们知道,定义函数时有几个位置参数,调用时就必须给几个实参,不能多也不能少.有时候,实际应用过程中,参数往往不能固 ...

  2. C++随笔(1)——关于C++11中的线程创建,join和detach

    主要是和之前的博文有关,之前在这里有一部分代码是通过创建新的进程来应对新的用户请求的,但是基本没怎么解释怎么用的,所以这里做点小笔记. join 首先引入的库: #include <thread ...

  3. 24个MySQL面试题

    一.为什么用自增列作为主键? 1.如果我们定义了主键(PRIMARY KEY),那么InnoDB会选择主键作为聚集索引. 如果没有显式定义主键,则InnoDB会选择第一个不包含有NULL值的唯一索引作 ...

  4. 【计算机视觉】阶编码本模型(Multi phase codebook model)

    转自:http://www.cnblogs.com/xrwang/archive/2012/04/24/MPCBBGM.html 多阶编码本模型(Multi phase codebook model) ...

  5. Java网络爬虫

    一.前言 首先我们把准备工作做好:IDEA 2019.1.JDK1.8.Maven3.5 Jsoup的Maven依赖: <dependency> <groupId>org.js ...

  6. 软件设计分为结构化设计(SD)

    软件设计分为结构化设计(SD)与面向对象设计(OOD). 其中结构化设计SD是一种面向数据流的方法,它以SRS(软件需求规格说明书)和SA(结构化分析)阶段所产生的和数据字典等文档为基础,是一个自顶向 ...

  7. sql回显注入-笔记

     拼接sql命令查询数据   注释 常用于sql注入            # 井号 单行注释 注意:URL编码 %23          -- 两个减号加空格 单行注释           /*   ...

  8. windows jenkins 发布 springboot项目脚本

    windows  jenkins 发布 springboot项目脚本 1.关闭现有程序 (按端口关闭) [与按应用关闭 二选一] @echo off for /f "tokens=1-5&q ...

  9. spring boot-18.使用dubbo发布分布式服务

    我们新建两个项目分别模拟服务的提供者和服务的消费者,spring boot 集成dubbo主要分为以下几个步骤: 1.安装zookeeper 推荐使用docker 安装,使用以下几个命令即可完成 (1 ...

  10. mysql(中)

    mysql(中) 一.数据库配置 1.登录mysql 2.在mysql安装目录下:创建my.ini(my.cnf) 3.设置配置信息并保存 ''' [mysqld] character-set-ser ...