当前位置:网站首页>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 <= 20000
S
consists 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)
边栏推荐
猜你喜欢
Chengdu | Changed to software testing, from zero income to over 10,000 monthly salary, a new turning point in life...
Typescript使用修饰器混合方法到类
What should I do if the Win11 network is unstable?The solution to frequent disconnection of wifi connection in Win11
第01章 Linux下MySQL的安装与使用【1.MySQL架构篇】【MySQL高级】
Typescript mix method to class with decorator
牛客网刷题记录 || 指针
函数的参数
小程序组件的总结
【技术课堂】从批到流:pull or not pull, that's a question
AI全流程开发难题破解之钥
随机推荐
微信支付接口
Flink1.15源码阅读flink-clients之GenericCLI、flinkYarnSessionCLI和DefaultCLI
TDengine 助力西门子轻量级数字化解决方案
不用Swagger,那我用啥?
2022暑假 动态规划总结
无代码开发平台角色设置入门教程
The significance of knowledge base to enterprises
低代码三部曲之未来
C # CLI (common language infrastructure)
笛卡尔树(暑假每日一题 9)
Typescript类功能混合(mixin)使用,将多个类中功能合并到一个对象
基于STM32的RFID-RC522门禁系统
第02章 MySQL的数据目录【1.MySQL架构篇】【MySQL高级】
如何使用TDengine Sink Connector?
R语言使用zoo包表示时间序列数据(time series data)
经典SQL语句大全
cv2 imread()函数[通俗易懂]
uniapp H5 跳转应用外页面
pfSense high availability (HA) function introduction
PromptBERT: Improving BERT Sentence Embeddings with Prompts