728x90
난이도 : easy
문자열 내에 숫자가 있다면, 숫자 및 숫자에 가장 가까운 왼쪽 문자열(숫자 아님) 하나를 같이 지우는 문제
import re
class Solution:
def clearDigits(self, s: str) -> str:
while re.findall(r'\d',s):
s = re.sub(r'\D\d', '', s)
return s
- re를 import 후, re.findall로 s에 숫자가 있는지 확인
- re.sub으로 \D(숫자가 아닌 것) \d(숫자)를 지우기
Runtime : 4ms (10.63%)
Memory : 17.84MB (27.74%)
728x90
'코딩 공부 > Leetcode' 카테고리의 다른 글
[Leetcode / Python] 3066. Minimum Operations to Exceed Threshold Value II (0) | 2025.02.13 |
---|---|
[Leetcode / Python] 2364. Count Number of Bad Pairs (0) | 2025.02.13 |
[Leetcode / Python] 1910. Remove All Occurrences of a Substring (0) | 2025.02.11 |
[Python] 206. Reverse Linked List (0) | 2025.02.11 |
[Python] 19. Remove Nth Node From End of List (0) | 2025.02.10 |