blob: c7cbfb5884682aeee9487a2db46395762b6c9867 (
plain)
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
31
32
33
34
35
36
37
38
39
40
|
// 2390. Removing Stars From a String
#include "leetcode.h"
/*
* given a string 's', which contains stars (*). in one operation
* you can choose a start in 's', remove the closest non-star
* character to its left, as well remove the star itself.
* return string after all stars have been removed. note, the
* input will be generated such that the operation is always
* possible, and the resulting string will always be unique
*/
class Solution {
public:
string removeStars(string s) {
stack<char> stk;
string ans;
vector<char> vec;
for (int i = 0; i < s.size(); i++) {
if (s[i] != '*')
stk.emplace(s[i]);
else
stk.pop();
}
while (!stk.empty()) {
vec.emplace_back(stk.top());
stk.pop();
}
reverse(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); i++)
ans += vec[i];
return ans;
}
};
int main() {
Solution obj;
cout << obj.removeStars("leet**cod*e") << endl; // expect: lecoe
cout << obj.removeStars("erase*****") << endl; // expect: null
}
|