![[Leetcode] 2390 Removing Starts From a String (Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdna%2Fd2NN6o%2FbtsQMjuwtXL%2FAAAAAAAAAAAAAAAAAAAAAMnb0-Q9yu6ugc9uhf3MLZHCLnsbqAOyV8RpxtDCQdFz%2Fimg.png%3Fcredential%3DyqXZFxpELC7KVnFOS48ylbz2pIh7yKj8%26expires%3D1761922799%26allow_ip%3D%26allow_referer%3D%26signature%3DwLAmYf4CpYcfL75MQK9fTY55YSc%253D)
[Leetcode] 2390 Removing Starts From a String (Python)leetcode2025. 9. 24. 13:11
목차
문제
Removing Stars From a String - LeetCode
설명
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.
풀이 계획
s를 돌면서 빈 배열에 한 글자씩 담는다. *이 나올 경우 배열을 pop시켜서 마지막 요소를 꺼낸다.
초기 풀이 (151ms, 19.03MB)
class Solution:
def removeStars(self, s: str) -> str:
res = []
for i in range(len(s)):
if s[i] == '*':
res.pop()
else:
res.append(s[i])
return ''.join(res)
시간복잡도 : O(N)
공간복잡도 : O(N)
728x90
반응형
'leetcode' 카테고리의 다른 글
[Leetcode] 724 Find Pivot Index (Python) (0) | 2025.09.29 |
---|---|
[Leetcode] 151 Reverse Words in a String (Python) (0) | 2025.09.23 |
[Leetcode] 1732 Find the Highest Altitude (Python) (0) | 2025.09.22 |
[Leetcode] 1456 Maximum Number of Vowels in a Substring of Given Length (Python) (0) | 2025.09.17 |
[Leetcode] 392 Is Subsequence (Python) (0) | 2025.09.16 |
@kdj :: Childev'note
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!