---
# System prepended metadata

title: 2390. Removing Stars From a String
tags: [Leetcode, Medium, Stack]

---

# 2390. Removing Stars From a String
###### tags: `Leetcode` `Medium` `Stack`
Link: https://leetcode.com/problems/removing-stars-from-a-string/
## 思路
stack 每次遇到```*```就pop
## Code
```java=
class Solution {
    public String removeStars(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i=0; i<s.length(); i++){
            if(s.charAt(i)=='*'){
                if(!stack.isEmpty()) stack.pop();
            }
            else{
                stack.push(s.charAt(i));
            }
        }
        StringBuilder sb = new StringBuilder();
        while(!stack.isEmpty()) sb.append(stack.pop());
        return sb.reverse().toString();
    }
}
```