当前位置:网站首页>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();
}
}
边栏推荐
- 【LOJ3247】「USACO 2020.1 Platinum」Non-Decreasing Subsequences(DP,分治)
- How to deal with being attacked? Advanced anti DDoS IP protection strategy
- Dynamics crm: mailbox configuration (III) - configure email server profiles and mailboxes
- Varnish4.0 cache agent configuration
- Configuring WAPI certificate security policy for Huawei wireless devices
- Class assignment (6) - 575. Word division (word)
- 机器学习笔记 - 构建推荐系统(5) 前馈神经网络用于协同过滤
- Vscode common shortcut keys
- 降噪蓝牙耳机哪个好?性价比最高的降噪蓝牙耳机排行
- Leetcode 223. 矩形面积
猜你喜欢

Yolov6 trains its own data set

Hard core innovation that database needs to care about in the future

Talk about C pointer

Small list of iptables common commands

Dedecms editor supports automatic pasting of word pictures

【SWT】滚动容器实现商品列表样式

【AdaptiveAvgPool3d】pytorch教程

Fine tune layoutlm V3 for bill data processing and content recognition

Leetcode 223. rectangular area

Dynamics crm: how to set the order of forms
随机推荐
Introduction to bermudagrass
Force button 31. Next arrangement -- double finger needling
From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
yolov6训练自己的数据集
Leetcode 231. 2 的幂
Yolov6 trains its own data set
[acwing] 909. Chess game
Pattern water flow lamp 1: check the table and display the LED lamp
Mlx90640 infrared thermal imager temperature measurement module development notes (III)
Choice of advanced anti DDoS IP and CDN in case of DDoS
Arduino ide esp32 firmware installation and upgrade tutorial
Knowledge points of MySQL (12)
memcache缓存应用(LNMP+memcache)
2.19 haas506 2.0 development tutorial - Bluetooth - Bluetooth communication (only supports versions above 2.2)
Power of leetcode 231.2
Who is the "roll" king of the prefabricated vegetable track?
Machine learning notes - building a recommendation system (5) feedforward neural network for collaborative filtering
Arduino IDE ESP32固件安装和升级教程
105 constructing binary trees from preorder and inorder traversal sequences
Memcache cache application (lnmp+memcache)