[M] 2390. Removing Stars From a String

YJ·2025년 4월 16일

leetcode

목록 보기
32/100

https://leetcode.com/problems/removing-stars-from-a-string

문제

을 포함한 String 이 주어지면, 와 그 바로 왼쪽 character 를 지울 수 있다. *가 모두 지워졌을 때의 결과값을 반환.

답안

public String removeStars(String s) {
	Stack<Character> st = new Stack<>();
	for (char ch : s.toCharArray()) {
		if (ch == '*') {
			st.pop();
			continue;
		}
		st.push(ch);
	}
	return st.stream()
    	.map(c -> c.toString())
        .collect(Collectors.joining(""));
    }

stack 써서 * 이면 pop, 아니면 push

메모

profile
Hello

0개의 댓글