题目如下:

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. jquery实现分页效果

    通过jq实现分页的原理如下:将所有条数加载到页面中,根据每页放特定条数(例如 5 条)获取jquery对象索引,使部分条数显示,其他条数隐藏. 前提:引入jquery.js 代码 <!DOCTY ...

  2. 交换机安全学习笔记 第四章 VLAN

      Trunk 口  思科称为:native VLAN  华为称为:PVID   说白了就是Trunk端口本身所属的VLAN,因为,Trunk端口要"透传"多个VLAN的流量,其本 ...

  3. 【Linux 网络编程】TCP/IP四层模型

    应用层.传输层.网络层.链路层 链路层:常用协议 ARP(将物理地址转化为IP地址) RARP(将IP地址转换为物理地址) 网络层(IP层):重要协议ICMP IP IGMP 传输层:重要的协议TCP ...

  4. Vue.js官方文档学习笔记(二)组件化应用的构建

    组件化应用的构建 组件化应用允许我们使用小型.独立和通常可复用的组件构建大型应用. Vue注册组件 Vue.component('todo-item',{template:'<li>这是个 ...

  5. springBoot2.0配置profile

    1. 使用yaml来配置,直接配置application.yml文件 server: port: 8888 spring: profiles: active: dev # 激活生产环境 --- # 测 ...

  6. scrapy之360图片爬取

    #今日目标 **scrapy之360图片爬取** 今天要爬取的是360美女图片,首先分析页面得知网页是动态加载,故需要先找到网页链接规律, 然后调用ImagesPipeline类实现图片爬取 *代码实 ...

  7. Python Set intersection() 方法

    描述 intersection() 方法用于返回两个或更多集合中都包含的元素,即交集. 语法 intersection() 方法语法: set.intersection(set1, set2 ... ...

  8. .net core 调用webservice

    原文:.net core 调用webservice 1.点击core项目添加链接的服务 2.键入对应的webservice地址,下载对应的代理服务 4.由于.net core  代理类只支持异步方法  ...

  9. JDK1.8+API+中文文档+高清完整版(不要积分 免费拿)

    JDK1.8+API+中文文档+高清完整版+CHM帮助文档 链接: https://pan.baidu.com/s/1LbdWSZ4qFjWXdJ88bXkn5w 提取码: frew 希望能帮上大家的 ...

  10. html元素标签时间格式化

    <fmt:formatDate value="${user.loginTime}" pattern="yyyy-MM-dd HH:mm:ss"/>