Skip to content

Commit 694a277

Browse files
committed
Time: 339 ms (9.71%), Space: 32.3 MB (90.3%) - LeetHub
1 parent c26f1dc commit 694a277

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
string robotWithString(string s) {
4+
vector<int> f(26);
5+
for (auto& i : s) f[i - 'a']++;
6+
stack<char> stk;
7+
string ans;
8+
for (auto& i : s) {
9+
f[i - 'a']--;
10+
stk.push(i);
11+
while (!stk.empty()) {
12+
bool ok = true;
13+
for (int j = 0; j < stk.top() - 'a'; j++) {
14+
if (f[j]) ok = false;
15+
}
16+
if (!ok) break;
17+
ans += stk.top();
18+
stk.pop();
19+
}
20+
}
21+
return ans;
22+
}
23+
};

0 commit comments

Comments
 (0)