Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 이진탐색
- 아파치 하둡
- Apache Hadoop
- apache kafka
- BFS
- 코딩테스트
- 우선순위큐
- 알고리즘
- Kubernetes
- String
- 그래프
- heapq
- 문자열
- 분산
- 리트코드
- 파이썬
- 오블완
- docker
- programmers
- 티스토리챌린지
- Apache Spark
- 도커
- 아파치 카프카
- 아파치 스파크
- leetcode
- DP
- 프로그래머스
- 쿠버네티스
- Python
- 분산처리
Archives
- Today
- Total
래원
[LeetCode] 3174. Clear Digits (Python) 본문
난이도: Easy
문제 설명
You are given a string s
.
Your task is to remove all digits by doing this operation repeatedly:
- Delete the first digit and the closest non-digit character to its left.
Return the resulting string after removing all digits.
문제 예제
Example 1:
Input: s = "abc"
Output: "abc"
Explanation:
There is no digit in the string.
Example 2:
Input: s = "cb34"
Output: ""
Explanation:
First, we apply the operation ons[2]
, ands
becomes"c4"
.
Then we apply the operation ons[1]
, ands
becomes""
.
제한 사항
1 <= s.length <= 100
s
consists only of lowercase English letters and digits.- The input is generated such that it is possible to delete all digits.
✏️ Solution(솔루션)
class Solution:
def clearDigits(self, s: str) -> str:
stack = []
n = len(s)
for i in range(n):
if not s[i].isdigit():
stack.append(s[i])
else:
if stack:
stack.pop()
return ''.join(stack)
이 문제는 s에 숫자가 나오면 왼쪽에 가장 근처 알파벳이랑 같이 지웠을 때 남는 요소들을 반환하면 되는 문제였다.
stack을 이용해 간단히 해결할 수 있다.
s의 요소가 알파벳이라면 stack에 넣어준다.
만약 s의 요소가 숫자라고 한다면 stack의 마지막 요소를 pop해준다.
결국 stack 마지막 요소가 제일 가까운 왼쪽 알파벳이기 때문이다.
모든 요소를 탐색하고 stack에 남아있는 알파벳들을 반환하고 정답을 맞출 수 있었다.
아래 그림은 간단한 예시를 보여준다.
깃허브: github
algorithmPractice/LeetCode/3447-clear-digits at main · laewonJeong/algorithmPractice
하루 한 문제 챌린지. Contribute to laewonJeong/algorithmPractice development by creating an account on GitHub.
github.com
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode] 1910. Remove All Occurrences of a Substring (Python) (0) | 2025.02.11 |
---|---|
[LeetCode] 2364. Count Number of Bad Pairs (Python) (0) | 2025.02.09 |
[LeetCode] 2349. Design a Number Container System (Python) (0) | 2025.02.08 |
[LeetCode] 3160. Find the Number of Distinct Colors Among the Balls (Python) (0) | 2025.02.07 |
[LeetCode] 1726. Tuple with Same Product (Python) (0) | 2025.02.06 |