-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2434.cpp
More file actions
29 lines (29 loc) · 740 Bytes
/
2434.cpp
File metadata and controls
29 lines (29 loc) · 740 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
class Solution {
public:
int minFreq(vector<int>& freq) {
for (int i = 0; i < 26; ++i) {
if (freq[i] > 0) return i;
}
return 0;
}
string robotWithString(string s) {
int n = s.size();
vector<int> freq(26, 0);
for (auto& c : s) freq[c - 'a']++;
stack<int> st;
string res;
for (auto& c : s) {
st.push(c - 'a');
freq[c - 'a']--;
while (!st.empty() && st.top() <= minFreq(freq)) {
res.push_back(st.top() + 'a');
st.pop();
}
}
while (!st.empty()) {
res.push_back(st.top() + 'a');
st.pop();
}
return res;
}
};