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.
class Solution {
String removeStars(String s) {
List<String> answer = [];
for(int i = 0; i < s.length; i++){
if(s[i] != "*"){
answer.add(s[i]);
} else {
answer.removeLast();
}
}
return answer.join();
}
}
class Solution {
String removeStars(String s) {
String answer = "";
for(int i = 0; i < s.length; i++){
if(s[i] != "*"){
answer += s[i];
} else {
answer = answer.substring(0, answer.length - 1);
}
}
return answer;
}
}