当前位置:网站首页>Stack and queue - 1047. Delete all adjacent duplicates in the string
Stack and queue - 1047. Delete all adjacent duplicates in the string
2022-07-24 16:00:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Give a string of lowercase letters S, Duplicate deletion selects two adjacent and identical letters , And delete them .
stay S Repeat the delete operation on , Until you can't delete .
Returns the final string... After all the duplicates have been deleted . The answer is guaranteed to be unique .
2 Title Example
Input :“abbaca”
Output :“ca”
explain :
for example , stay “abbaca” in , We can delete “bb” Because two letters are adjacent and the same , This is the only duplicate that can be deleted at this time . And then we get the string “aaca”, There is only “aa” You can perform a duplicate deletion operation , So the last string is “ca”.
3 Topic tips
1 <= S.length <= 20000
S It's only made up of lowercase letters .
4 Ideas
After fully understanding the meaning of the question , We can find out , When there are multiple groups of adjacent duplicates in a string at the same time , No matter which one we delete first , Will not affect the final result . So we can process the string in turn from left to right .
And eliminate — For adjacent duplicates, new adjacent duplicates may appear , Such as from string abba Delete in bb Will result in new adjacent duplicates aa appear . So we need to save the characters that haven't been deleted yet . An obvious data structure is just around the corner : Stack . We just need to traverse the string , If the current character is the same as the top of the stack , We greedily eliminate it , Otherwise, put it on the stack .
Complexity analysis
· Time complexity :O(n), among n Is the length of the string . We only need to traverse the string once .
Spatial complexity :O(n) or o(1), It depends on whether the string class provided by the language used provides a similar 「 Push 」 and 「 Out of the stack 」 The interface of . Note that the return value is not included in the spatial complexity .
5 My answer
class Solution {
public String removeDuplicates(String s) {
StringBuffer stack = new StringBuffer();
int top = -1;
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
if (top >= 0 && stack.charAt(top) == ch) {
stack.deleteCharAt(top);
--top;
} else {
stack.append(ch);
++top;
}
}
return stack.toString();
}
}
边栏推荐
- Dynamics crm: how to set the order of forms
- mysql源码分析——索引的数据结构
- Research on the efficiency of numpy array access
- Withdrawal of IPO application, Yunzhou intelligent "tour" of unmanned boat enterprise fails to enter the science and Technology Innovation Board
- Exomeiser annotates and prioritizes exome variants
- Pattern water flow lamp 1: check the table and display the LED lamp
- Dynamics crm: sharing records for users and teams
- Introduction to single chip microcomputer: LED lights cycle to the left and turn on
- Windows10 installation free redis
- IP protocol - network segment division
猜你喜欢

yolov3 训练自己的数据集

From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?

【LOJ3247】「USACO 2020.1 Platinum」Non-Decreasing Subsequences(DP,分治)

Dedecms editor supports automatic pasting of word pictures

Error: pidfile (celerybeat.pid) already exists when celery starts beat

124 maximum path sum in binary tree

机器学习笔记 - 构建推荐系统(5) 前馈神经网络用于协同过滤

Windows10 installation free redis

Power of leetcode 231.2

Who is the "roll" king of the prefabricated vegetable track?
随机推荐
机器学习笔记 - 构建推荐系统(5) 前馈神经网络用于协同过滤
Kubernetes版本对接对象存储
[leetcode] day103 search two-dimensional matrix II
PHP中array_merge的坑
105 constructing binary trees from preorder and inorder traversal sequences
[tf.keras]: a problem encountered in upgrading the version from 1.x to 2.x: invalidargumenterror: cannot assign a device for operation embedding_
Who is the "roll" king of the prefabricated vegetable track?
Research on the efficiency of numpy array access
yolov6训练自己的数据集
【洛谷】P1908 逆序对
JUC源码学习笔记3——AQS等待队列和CyclicBarrier,BlockingQueue
Introduction to single chip microcomputer: LED lights cycle to the left and turn on
Some understanding of the rank sum of matrix and the rank of image
简化理解:发布订阅
未来数据库需要关心的硬核创新
有了这个机器学习画图神器,论文、博客都可以事半功倍了!
YOLO5Face:为什么要重新发明人脸检测器
SQL row to column, column to row
Error 1053: the service did not respond to the start or control request in a timely fashion
Dynamics 365: explain virtual entity from 0 to 1