[LeetCode] Removing Stars From a String

2390. Removing Stars From a String

You are given a string s, which contains stars *.

In one operation, you can:

  • Choose a star in s.
  • Remove the closest non-star character to its left, as well as remove the star itself.

Return the string after all stars have been removed.

Note:

  • The input will be generated such that the operation is always possible.
  • It can be shown that the resulting string will always be unique.
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
string removeStars(string s) {
string res = "";
for(auto& ch : s) {
if(ch == '*') {
if(!res.empty()) res.pop_back();
} else res.push_back(ch);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/28/PS/LeetCode/removing-stars-from-a-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.