当前位置:网站首页>LeetCode 1047 Remove all adjacent duplicates in a string
LeetCode 1047 Remove all adjacent duplicates in a string
2022-07-29 20:04:00 【Gordennizaicunzai】
1047.Remove all adjacent duplicates in a string
Simple Difficulty 410 Favorites and Sharing Switch to English to receive dynamic feedback
Given the string S consisting of lowercase letters, the duplicate removal selects two adjacent and identical letters and deletes them.
Repeat deduplication on S until no further deletions are possible.
Returns the final string after all deduplication operations are done.The answer is guaranteed to be unique.
Example:
Enter:"abbaca"Output:"ca"Explanation:For example, in "abbaca", we can delete "bb" since the two letters are adjacent and identical, this is the only duplicate that can be deleted at this time.Then we get the string "aaca", of which only "aa" can perform the deduplication operation, so the final string is "ca".
Tip:
1 <= S.length <= 20000Sconsists of lowercase English letters only.
class Solution:def removeDuplicates(self, s: str) -> str:# Use the stack, compare the ones that are not on the stack with the top of the stack, if they are the same, they will pop, and if they are not, they will be pushed into the stack.lst = []for v in s:if lst and lst[-1] == v:lst.pop()else:lst.append(v)return "".join(lst)
边栏推荐
猜你喜欢

Really touch the fish and lead the teacher: The programmer brother works 10 minutes a day with an annual salary of 570,000. I broke the defense...

不堆概念、换个角度聊多线程并发编程

H264码流RTP封装方式详解

效率技巧│十分钟学会 xmind 思维导图的使用

What should I do if the Win11 network is unstable?The solution to frequent disconnection of wifi connection in Win11

牛客网剑指offer刷题练习之重构二叉树

Win11任务栏太宽了怎么变窄?Win11任务栏宽度调整方法

Postgresql-xl全局快照代码走读与GTM原理(支线1)

优雅实现经典的生产者消费者模式

低代码搭建高效管理房屋检测系统案例分析
随机推荐
PIL库和opencv库
高速无源链路阻抗匹配套路
关于Image scaleType的属性详解,以及每一个属性的区别
cv2 imread()函数[通俗易懂]
总数据量超万亿行,玉溪卷烟厂通过正确选择时序数据库轻松应对
The significance of knowledge base to enterprises
Idea工具的使用
真·摸鱼带师:程序员小哥每天工作10分钟年薪57万,我破防了...
yarn的安装和使用(yarn安装mysql)
Apifox免费吗?完全免费,不限团队人数,不限功能
2.5w字 + 36 张图爆肝操作系统面试题 学不吐你
【中标麒麟系统Your trial is EXPIRED and no VALID licens 关闭弹窗】
中天钢铁在 GPS、 AIS 调度中使用 TDengine
PromptBERT: Improving BERT Sentence Embeddings with Prompts
一些投资的底线
7 lines of code crashed station B for 3 hours, but because of "a tricky 0"
牛客网剑指offer刷题练习之重构二叉树
R语言时间序列数据提取:使用xts包的first函数提取时间序列中最前面10天的数据(first 10 day)
洪九果品、百果园抢滩港股,卖水果是门好生意吗?
小程序组件的总结