diff options
Diffstat (limited to 'remove_star_string.cpp')
-rw-r--r-- | remove_star_string.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/remove_star_string.cpp b/remove_star_string.cpp new file mode 100644 index 0000000..c7cbfb5 --- /dev/null +++ b/remove_star_string.cpp @@ -0,0 +1,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 +} |