-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathleetcode2390_removing_starts_from_a_string.cpp
More file actions
57 lines (48 loc) · 1.12 KB
/
leetcode2390_removing_starts_from_a_string.cpp
File metadata and controls
57 lines (48 loc) · 1.12 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* leetcode2390_removing_starts_from_a_string.cpp
*
* @author Wang Guibao (wang_guibao@163.com)
* @date 2023/10/09 16:40
* @brief https://leetcode.com/problems/removing-stars-from-a-string
*/
#include <iostream>
#include <stack>
using namespace std;
class Solution {
public:
string removeStars(string s) {
std::stack<char> st;
int len = s.length();
for (int i = 0; i < len; ++i) {
if (s[i] == '*') {
st.pop();
} else {
st.push(s[i]);
}
}
std::stack<char> st2;
while (!st.empty()) {
st2.push(st.top());
st.pop();
}
std::string retStr;
while (!st2.empty()) {
retStr += st2.top();
st2.pop();
}
return retStr;
}
};
int main() {
while (1) {
std::string word;
std::cout << "Input word: ";
if (!std::getline(std::cin, word)) {
return 0;
}
Solution solution;
auto ret = solution.removeStars(word);
std::cout << ret << std::endl;
}
return 0;
}