코딩 공부/Leetcode

[Leetcode / Python] 3174. Clear Digits

일하는 공학도 2025. 2. 12. 15:28
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
  1. re를 import 후, re.findall로 s에 숫자가 있는지 확인
  2. re.sub으로 \D(숫자가 아닌 것) \d(숫자)를 지우기

Runtime : 4ms (10.63%)

Memory : 17.84MB (27.74%)

728x90