-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY0076.cpp
More file actions
30 lines (30 loc) · 789 Bytes
/
DAY0076.cpp
File metadata and controls
30 lines (30 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 2434. Using a Robot to Print the Lexicographically Smallest String
class Solution {
public:
string robotWithString(string s) {
vector<int>hashmap(26,0);
int size=s.length();
for(int i=0;i<size;i++){
hashmap[s[i]-'a']++;
}
string answer;
stack<char> t;
char small='a';
for(char c:s){
hashmap[c - 'a']--;
while (small <= 'z' && hashmap[small - 'a'] == 0) {
small++;
}
t.push(c);
while (!t.empty() && t.top() <= small) {
answer += t.top();
t.pop();
}
}
while (!t.empty()) {
answer += t.top();
t.pop();
}
return answer;
}
};