当前位置:网站首页>Buckle practice - 24 remove repeated letters
Buckle practice - 24 remove repeated letters
2022-07-24 12:12:00 【qq_ forty-three million four hundred and three thousand six hun】
24 Remove duplicate letters
1. Problem description
Give you a string containing only lowercase letters , Please remove the duplicate letters from the string , Make each letter appear only once . Ensure that the dictionary order of the returned results is the smallest ( The relative position of other characters should not be disturbed ).
Example 1:
Input : “bcabc”
Output : “abc”
Example 2:
Input : “cbacdcbc”
Output : “acdb”
2. Enter description
Enter a string containing only lowercase letters
3. The output shows that
Output results . There are no extra spaces or blank lines at the beginning and end .
4. Example
Input
cbacdcbc
Output
acdb
5. Code
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
string removeDuplicateLetters(string s)
{
// Subject requirements :
// Remove duplicate letters from the string , Make each letter appear only once
// Ensure that the dictionary order of the returned results is the smallest ( The relative position of other characters should not be disturbed ).
// Their thinking :
//1. First, sort according to the dictionary order , So greedy thoughts
//2. Because the relative order cannot be changed , Therefore, we need to consider the dictionary order on the premise of ensuring that each letter appears at least once
//3. Use monotone stack to ensure dictionary sequence ( From small to large ), Monotonic stack : Non increasing or non decreasing stack .
//4. The conditions for the stack top elements to be out of the stack should be limited , Only when the stack is not empty And The dictionary order of the elements at the top of the stack is after the new elements currently traversed And When the stack top element will appear later , Out of the stack
//1. Declare variables
vector<int>num(26, 0);// Notice the initialization method here , Statistics a~z The number of times the letter appears , The initial values are all 0
vector<bool>exist(26, false);// Used to count whether the element exists in the stack
//2. Count the number of occurrences of each character
for (int i = 0; i < s.size(); i++)
num[s[i] - 'a']++;
//3. Traversal string , Perform stack in and stack out operations
string ans = "";// Use this simulation stack
for (int i = 0; i < s.size(); i++)
{
if (!exist[s[i] - 'a'])// Never existed in the stack
{
// Focus on understanding the following code The element at the top of the stack is ans.back()
// Stack is not empty. && The top element of the stack is at the end of the dictionary && There are still remaining occurrences of the top element
while (ans.size()!=0 && ans.back()>s[i] && num[ans.back()-'a']>0 )
{
exist[ans.back() - 'a'] = false;// Modify the appearance status
ans.pop_back();// Out of the stack
}
ans.push_back(s[i]);// The letters in this position are pressed
exist[s[i] - 'a'] = true;// Modify the appearance status
}
num[s[i]-'a']--;// Every time you traverse a letter , The number of occurrences of this letter is reduced by one
}
return ans;
}
int main()
{
string s;
cin >> s;
string res = removeDuplicateLetters(s);
cout << res;
return 0;
}
边栏推荐
- Why is the datetime type field 8 hours earlier when I use flinkcdc to read MySQL's binlog?
- A* and JPS
- 6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
- JVM visualvm: multi hop fault handling tool
- L1-064 估值一亿的AI核心代码
- Delphi gexperts expert instructions for improving development efficiency
- Convergence rules for 4 * 4 image weights
- Use and expansion of fault tolerance and fusing
- 生信周刊第37期
- 20000 words detailed explanation, thoroughly understand es!
猜你喜欢

QT notes - qtxml

L1-059 ring stupid bell

String matching KMP

2 万字详解,吃透 ES!
![Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]](/img/a9/f080940ec7bf94ab83c922990efa62.png)
Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]

C Advanced - data storage

Pushgateway installation and Prometheus configuration

How to find out the function calling process of complex code running as soon as possible

三层交换机配置MSTP协议详解【华为eNSP实验】

Svn server and client installation (Chinese package) and simple use
随机推荐
An analysis of the CPU surge of an RFID tag management system in.Net
L1-064 估值一亿的AI核心代码
Mysql database
Open source DNS software powerdns BIND9 mydns
Why is the datetime type field 8 hours earlier when I use flinkcdc to read MySQL's binlog?
生信周刊第37期
Literature record (part109) -- self representation based unsupervised exemplar selection in a union of subspaces
一文看懂MES系统能实现企业哪些目标
Learning materials about red team
Script redis write project notes
1184. 公交站间的距离 : 简单模拟题
The difference between where and having
L1-059 ring stupid bell
[Commons beanautils topic] 004 beanautils topic
Understand what goals the MES system can achieve
GCC的基本用法
Svn server and client installation (Chinese package) and simple use
Understand the storage and retrieval of data
L2-011 play with binary tree
JVM visualvm: multi hop fault handling tool