【leetcode】1209. Remove All Adjacent Duplicates in String II
题目如下:
Given a string
s
, a k duplicate removal consists of choosingk
adjacent and equal letters froms
and removing them causing the left and the right side of the deleted substring to concatenate together.We repeatedly make
k
duplicate removals ons
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的更多相关文章
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- 【leetcode】1047. Remove All Adjacent Duplicates In String
题目如下: Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent a ...
- 【Leetcode_easy】1047. Remove All Adjacent Duplicates In String
problem 1047. Remove All Adjacent Duplicates In String 参考 1. Leetcode_easy_1047. Remove All Adjacent ...
- LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)
1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- LeetCode 1047. Remove All Adjacent Duplicates In String
1047. Remove All Adjacent Duplicates In String(删除字符串中的所有相邻重复项) 链接:https://leetcode-cn.com/problems/r ...
- 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 ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
随机推荐
- SQLALchemy如何查询mysql某个区间内的数据
查了下,找到3种方式: 方法一注意时间格式:xxxx-xx-xx 方法二没有‘day’ 方法三的时间格式同方法一 1.result = Jobs.query.filter(Jobs.create_ti ...
- epoll 性能分析(解决占用CPU 过高问题)
针对自己写的一个服务器网络引擎Engine 文章后面附上源码 使用epoll 刚刚开始时候发现占用CPU 特别高,但是网络引擎里面基本没干什么事,不应该有这么高的CPU,一直不解, 于是自己慢慢的分 ...
- css3实现倾斜转动的转盘
HTML代码: <div class="r-1">a</div> <div class="r-2">a</div> ...
- Prometheus Querying Function rate() vs irate()
rate() rate(v range-vector) calculates the per-second average rate of increase of the time series in ...
- 几个关于json序列化 的注解
一.@JsonProperty 1.此注解用于属性上,作用是把该属性的名称序列化为另外一个名称.例如: @JsonProperty("name") private String N ...
- Linux 下面根据端口号 查询 可执行程序的路劲的方法
1. 安装上lsof 的包 2. 使用 lsof 命令查看相关进程 lsof -i: 效果为: 3. 根据/proc 的目录查看可执行目录的文件位置 ll /proc/procid # procid ...
- 模板 - 强连通分量 - Kosaraju
Kosaraju算法 O(n+m) vector<int> s; void dfs1(int u) { vis[u] = true; for (int v : g[u]) if (!vis ...
- Android热修复、插件化、组件化
模块化:项目按照独立的模块进行划分 组件化:将项目按照单一的组件来进行划分结构 项目组件化的重要环节在于,将项目按照模块来进行拆分,拆分成一个个业务module和其他支撑module(lib),各个业 ...
- phpstudy配置多站点
1.打开vhosts.conf文件 目录 Apache/conf/vhosts.conf #开启apache的vhost模块 (此模块默认是关闭的,去掉前面的#号) LoadModule vh ...
- doT学习(二)之用法集合
Advanced templating: illustrates defines and includes. Include external snippet defined in a variabl ...